PAT Grade B 1021 digit statistics 15 (points)

topic

Given a kk k -bit integer N = dk? 1 1 0 k? 1 +? + D 1 1 0 1 + d 0 N = d_{k-1}10^{k-1} + \cdots + d_1 10^ . 1 + D_0 N = D ? K ? . 1 ? ? . 1 0 ? K ? . 1 ? ? + ? + D ? . 1 ? ? . 1 0 ? . 1 ? ? + D ? 0 ? ? ( 0 ≤ DI ≤. 9 0 \ Le D_i \le 9 0 D ? I ? ? . 9 ,I = 0,?, K?. 1 I = 0, \ cdots, K-. 1 I = 0 , ? , K ? . 1 ,DK?. 1> 0 D_ {K-. 1}> 0 d ? k ? 1 ? ? > 0 ), write a program to count the number of each different digit arise. For example: GivenN = 1 0 0 3 1 1 N = 100311 N = 1 0 0 3 1 1 , there are 2 0s, 3 1s, and 1 3.

Input format:

Each input contains 1 test case, that is, a positive integer NN N with no more than 1000 digits .

Output format:

Of NN N each different numbers of bits to the digital format of the output bit in a row and in NN N number appears . Required by the ascending output. D:M D M D

Input sample:

100311

Sample output:

0:2
1:3
3:1

Code


#include<iostream>
using namespace std;
int main()
{
    
    
	string NUM = "0123456789", n;
	int cal[10] = {
    
     0 }, i, j;
	cin >> n;
	for (i = 0; n[i] != '\0'; i++)
		for (j = 0; j < 10; j++)
			if (n[i] == NUM[j])
				cal[j]++;
	for (i = 0; i < 10; i++)
		if (cal[i] != 0)
			cout << i << ":" << cal[i] << endl;
	return 0;
}

Question details link

Guess you like

Origin blog.csdn.net/qq_41985293/article/details/106317716