• HDU 1263 水果


    http://acm.hdu.edu.cn/showproblem.php?pid=1263

    第一题就这么的 令 (rang) 人 难 (tu) 忘 (xue)。。。

    //有错误请您大力指出,谢谢~

    主要是map的嵌套,定义了一个map < string, map < string, int >  > fru;

    分别用来对应水果名称、产地、数量。

    这里刚接触,不太懂。不过觉得这种嵌套的用法十分类似与二级指针。

    定义部分是这样的:

    map<string, map<string, int> > mp1;         //一个总的
    map<string, map<string, int> >::iterator ite1;  //迭代器 1号
    map<string, int> mp2;                 //用来调用嵌套的
    map<string, int>::iterator ite2;          //指向嵌套 迭代器 2号

    因为输入是两个字符串加一个整数

     int num;
     string s1, s2;
     cin>>s1>>s2>>num;
     mp1[s2][s1]+=num;
    View Code

    这里 mp1 [ s1 ] [ s2 ]大概可以类比着二级指针看吧,也就是二维数组的感觉。

    让某个地区"s2"对应的水果 "s1" 数量对应增加。而map初始化就是0,所以直接用。

    然后是输出。

    外层循环就是很普通的按照 map 的 "key"遍历。

    for(ite1=mp1.begin(); ite1!=mp1.end(); ite1++)
    View Code

    内层输出水果名称和地区时,让 ite2 指向 mp1 中 second 的 "key" ,并遍历。

    然后让 ite2 输出指向的第二部分中的一、二部分。

        for(ite2=(ite1->second).begirangn(); ite2!=(ite1->second).end(); ite2++){
                    cout<<"   |----"<<ite2->first<<'('<<ite2->second<<')'<<'
    ';
               }
    View Code

    最后根据循环控制变量 t 来判断是否换行。

    完整代码:

     1 #include <cmath>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <climits>
     6 #include <map>
     7 #include <set>
     8 #include <queue>
     9 #include <stack>
    10 #include <vector>
    11 #include <string>
    12 #include <iostream>
    13 #include <algorithm>
    14 
    15 #define N 100010
    16 
    17 using namespace std;
    18 
    19 typedef long long int ll;
    20 
    21 map<string, map<string, int> > mp1;
    22 map<string, map<string, int> >::iterator ite1;
    23 map<string, int> mp2;
    24 map<string, int>::iterator ite2;
    25 
    26 int main()
    27 {
    28     int t;
    29     scanf("%d", &t);
    30     while(t--){
    31         int n;
    32         scanf("%d", &n);
    33         while(n--){
    34             int num;
    35             string s1, s2;
    36             cin>>s1>>s2>>num;
    37             mp1[s2][s1]+=num;
    38         }
    39         for(ite1=mp1.begin(); ite1!=mp1.end(); ite1++){
    40             cout<<ite1->first<<endl;
    41             for(ite2=(ite1->second).begirangn(); ite2!=(ite1->second).end(); ite2++){
    42                 cout<<"   |----"<<ite2->first<<'('<<ite2->second<<')'<<'
    ';
    43             }
    44         }
    45         if(t!=0) cout<<'
    ';
    46         mp1.clear();
    47         mp2.clear();
    48     }
    49     return 0;
    50 }
  • 相关阅读:
    oracle的增删改查语句
    Oracle Rac11g 学习笔记
    怎么个下载别人网站上的字体icon图标fonts文件
    APICloud Studio 3和Apploader真机同步,同一个wifi却一直提示连接不到
    c#发qq邮箱,QQ邮箱当中开启“POP3/SMTP服务”获取授权码
    微信支付异步回调不返回xml,微信会重复多次异步通知
    阿里云C磁盘拓容,IIS日志记录开启关闭
    jsapi微信支付
    c#.Net手机网站对接支付宝接口对接全流程说明
    无法找到列 7 程序遍历超过Excel列数
  • 原文地址:https://www.cnblogs.com/Arrokoth/p/12181504.html
Copyright © 2020-2023  润新知