给定一个 k 位整数 N=dk−110k−1+⋯+d1101+d0(0≤di ≤9, i=0,⋯,k−1, dk−1
>0),请编写程序统计每种不同的个位数字出现的次数。例如:给定 N=100311,则有 2 个 0,3 个 1,和 1 个 3。
输入格式:
每个输入包含 1 个测试用例,即一个不超过 1000 位的正整数 N。
输出格式:
对 N 中每一种不同的个位数字,以 D:M 的格式在一行中输出该位数字 D 及其在 N 中出现的次数 M。要求按 D 的升序输出。
输入样例:
100311
输出样例:
0:2
1:3
3:1
#include <stdio.h>
#include <string.h>
int main()
{
char shuru[1001];
int count[10];
int len, i;
scanf("%s", shuru);
len = strlen(shuru);
for (i = 0; i < 10; i++)
{
count[i] = 0;
}
for (i = 0; i < len; i++)
{
if (shuru[i] == '0')
{
count[0] += 1;
}
if (shuru[i] == '1')
{
count[1] += 1;
}
if (shuru[i] == '2')
{
count[2] += 1;
}
if (shuru[i] == '3')
{
count[3] += 1;
}
if (shuru[i] == '4')
{
count[4] += 1;
}
if (shuru[i] == '5')
{
count[5] += 1;
}
if (shuru[i] == '6')
{
count[6] += 1;
}
if (shuru[i] == '7')
{
count[7] += 1;
}
if (shuru[i] == '8')
{
count[8] += 1;
}
if (shuru[i] == '9')
{
count[9] += 1;
}
}
for (i = 0; i< 10; i++)
{
if (count[i] != 0)
{
printf("%d:%d\n", i, count[i]);
}
}
return 0;
}
C++
#include <iostream>
#include <map>
using namespace std;
int main()
{
string s;
cin >> s;
map<char, int> m;
for(int i = 0; i < s.length(); i++)
{
m[s[i]]++;
}
for(auto it = m.begin(); it != m.end(); it++)
{
cout << it->first << ":" << it->second << endl;
}
return 0;
}