1.输出结果需要判定统计数是否为0,所以统计数和数字应该是匹配关系的,而且数字是0-9,所以想到用数组;
2.在统计数字时,则是只要下标出现一次,下标对应的值相应加一
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cin >> s;
int a[10] = {0};
int t = 0;
for(int i=0; i<s.length();i++)
{
t = s[i] - '0';//输入的是字符串,-‘0’后表示为数字
a[t]++;
}
for(int i=0;i<10;i++)
{
if(a[i]!=0)
cout << i << ":" << a[i] << endl;
}
return 0;
}