PAT B1021个位数统计--利用散列

1021 个位数统计 (15 分)

输入格式:
每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N。

输出格式:
对 N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。

输入样例:
100311
输出样例:
0:2
1:3
3:1

#include <iostream> 
#include<cstring>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	int hash[11]={0};
	char aa;
	while((aa=getchar())!='\n'){
		hash[aa-48]++;
	}
	for(int i=0;i<10;i++)
	if(hash[i]!=0) {
		printf("%d:%d\n",i,hash[i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43721351/article/details/84187313