Given a k -bit integer \ (N = d_ {k- 1} 10 ^ {k-1} + ⋯ + d_110 ^ 1 + d_0 (0≤d_i≤9, i = 0, ⋯, k-1, d_ { } 1-k> 0) \) , please write a program to count the number of each different digit arise. For example: given N = 100 311, there are two 0,3 1 2, and 1 3.
Input formats:
Each input comprises a test, i.e., no more than 1000 a positive integer N .
Output formats:
For N in each of the different digit to D:M
the digital format of the output bit in a row D
and the N number appears M
. Required by the D
ascending output.
Sample input:
100311
Sample output:
0:2
1:3
3:1
Thinking
- Use map container interior is sorted according to the key, the final output can traverse directly
Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<char,int> dict;
string s;
cin >> s;
map<char,int>::iterator it;
for(int i=0;i<s.size();i++)
{
it = dict.find(s[i]);
if(it == dict.end()) //没找到
{
dict[s[i]] = 1;
}else dict[s[i]] += 1;
}
for(it=dict.begin();it!=dict.end();it++)
printf("%c:%d\n", (*it).first, (*it).second);
return 0;
}
Quote
https://pintia.cn/problem-sets/994805260223102976/problems/994805300404535296