PAT B 1021. Single Digit Statistics (15)

topic here

1021. Single-digit statistics (15)

time limit
100 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

Given a k-bit integer N = d k-1 *10 k-1  + ... + d 1 *10 1  + d 0  (0<=d i <=9, i=0,...,k- 1, d k-1 >0), please write a program to count the number of occurrences of each different one-digit number. Example: Given N = 100311, there are 2 0s, 3 1s, and 1 3.

Input format:

Each input contains 1 test case, a positive integer N of no more than 1000 digits.

Output format:

For each different one-digit digit in N, output the digit D and the number M of occurrences in N in one line in the format of D:M. Requires output in ascending order of D.

Input sample:
100311
Sample output:
0:2
1:3
3:1

#include<cstdio>
#include<cstring>
int main(){
	char str[1005];
	gets(str);
	int len ​​= strlen (str), a;
	int count[10]={0};
	for(int i=0;i<len;i++){
		count[str[i]-'0']++;
	}
	for(int i=0;i<10;i++){
		if(count[i]!=0){
			printf("%d:%d\n",i,count[i]);
		}
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324449406&siteId=291194637