sicily--1486. 统计数字

  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;
		else
			printf("\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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值