当前位置:网站首页>Pat class B 1021 digit statistics

Pat class B 1021 digit statistics

2022-06-23 05:58:00 Octopus bro

1021. Single digit statistics (15)


Given a k An integer N = dk-1*10k-1 + ... + d1*101 + d0 (0<=di<=9, i=0,...,k-1, dk-1>0), Please write a program to count the number of times each different digit number appears . for example : Given N = 100311, Then there are 2 individual 0,3 individual 1, and 1 individual 3.

Input format :

Each input contains 1 Test cases , I.e. no more than one 1000 Bit positive integer N.

Output format :

Yes N Each different digit in , With D:M Output the digit in one line D And in N Is the number of times M. Required press D Ascending output of .

sample input :
100311
sample output :
0:2
1:3
3:1


Ideas : The number of digits is too large , Only strings can be used , First initialize a file with a length of 10 The array of is used as the count array , The following table corresponds to numbers , For each bit of the input string -‘0’, Take the result of this operation as the array subscript plus one operation , Finally, output

One 、 Starting variable

1.input【1000】 Input string

2.count【10】 Count array

Two 、 operation

1. Input string

2. Subtract each bit of the input string ‘0’ operation ,

3. Take the result of the operation as the index of the count array to perform the plus one operation

4. Cyclic output

3、 ... and 、 Code

#include "stdio.h"
#include "string.h"
int main()
{
	char input[1000];
	memset(input,0,1000);
	scanf("%s",input);
	int i;
	int count[10] = {0,0,0,0,0,0,0,0,0,0};// Corresponding 0-9 Number of occurrences of 
	for(i = 0; i < strlen(input); i++)
	{
		count[input[i] - '0']++;
	}
	for(i = 0; i < 10; i++)
	{
		if(count[i] != 0)
		{
			printf("%d:%d\n",i,count[i]);
		}
	}
	return 0;
}


原网站

版权声明
本文为[Octopus bro]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230410347690.html