Single digit statistics
Article Directory
Topic restatement
L1-003 Single digit statistics (15 points)
Please write a program to count the occurrences of each different single digit. For example: Given N=100311, there are 2 0s, 3 1s, and 1 3.
Input format:
Each input contains 1 test case, that is, a positive integer N with no more than 1000 digits.
Output format:
For each different one digit in N, output the digit D and the number of occurrences M in N in a line in the format of D:M. It is required to output in ascending order of D.
Input sample:
100311
Sample output:
0:2
1:3
3:1
code
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
int a[10]={
0};
cin>>s;
for(int i=0;i<s.length();i++)
a[s[i]-'0']++;
for(int j=0;j<10;j++)
{
if(a[j]!=0) cout<<j<<":"<<a[j]<<endl;
}
return 0;
}