map容器

试题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004

试题分析:本题如果使用预定义的map类会非常的方便

map定义:http://baike.baidu.com/view/4256069.htm?fr=aladdin

#include<iostream>

#include<map>

#include<string>



using namespace std;



int n,maxNum;

string a;

string maxColor;

map<string,int> ballon;

int main()

{



    while(cin>>n)

    {



        if(!n)break;

        maxNum = 0;

        ballon.clear();

        

       for(int i=0;i<n;i++){



    cin>>a;

                ballon[a]++;

        }

        map<string,int>::iterator ite;

        for(ite = ballon.begin();ite!=ballon.end();ite++){

            if(ite->second >maxNum){

                maxNum = ite->second;

                maxColor = ite->first;

            }

        }

        cout<<maxColor<<endl;

    }

    return 0;

}
 1 #include <iostream>

 2 #include <map>

 3 #include <string>

 4 

 5 using namespace std;

 6 string a,maxColor;

 7 map<string,int>zhangjie;

 8 int main(){

 9     int n,maxNum;

10     while(cin>>n&&n){

11         maxNum = 0;

12         zhangjie.clear();

13     for(int i = 0;i < n;i++){

14         cin>>a;

15         zhangjie[a]++;

16     }

17 

18     map<string,int>::iterator ite;

19     for(ite = zhangjie.begin();ite!=zhangjie.end();ite++)

20     {

21     if(ite->second > maxNum){

22     maxNum = ite->second;

23     maxColor = ite->first;

24     }

25     }

26     cout<<maxColor<<endl;

27     }

28     return 0;

29 }
模拟代码
 
   

 

 参考网址:http://blog.sina.com.cn/s/blog_4e7ae8ca0100xti7.html
 1 #include <iostream>

 2 #include <map>

 3 #include <string>

 4 

 5 using namespace std;

 6 map<string ,int>String;

 7 string zhangjie;

 8 int main()

 9 {

10     int n;

11     cin>>n;

12     while(n--){

13         cin>>zhangjie;

14         if(String[zhangjie])cout<<zhangjie<<String[zhangjie]<<endl;

15         else {cout<<"OK"<<endl;}

16         String[zhangjie]++;

17     }

18 

19     return 0;

20 }
比赛的map题解
 
   

 

 

 

你可能感兴趣的:(map)