sicily--1486. 统计数字

来源:互联网 发布:多媒体屏播放软件 编辑:程序博客网 时间:2024/05/22 15:52

  1. 绝对不难但又绝对坑人
  2. 用cin、cout 会超时
  3. 每个测试数据的输出之间还要空一行,最后一个测试数据不能有空行
  4. 用map 可以解决这个题目的排序问题, map内部使用的红黑树,会自动排序

//map 会自动排序#include<iostream>#include<cstdio>#include<map>using namespace std;int main(){bool flag = true;int num;while(cin >> num && num != 0)//题目也没说清楚到底是怎么结束{if(flag)flag = false;elseprintf("\n");map<int, int>counter;//关键字是数字, second是出现的次数for(int i = 0; i < num; i++){int temp;/*cin >> temp;*/scanf("%d", &temp);map<int, int>::iterator itCounter = counter.find(temp);//是否已出现if(itCounter == counter.end())//没出现过{counter[temp] = 1;//第一次}else//出现过{(*itCounter).second++;//次数+1}}//输出map<int, int>::iterator itCounter;for(itCounter = counter.begin(); itCounter != counter.end(); itCounter++){//cout << (*itCounter).first << " " << (*itCounter).second << endl;printf("%d %d\n", (*itCounter).first, (*itCounter).second);}}return 0;}


原创粉丝点击