PAT

1021 single-digit statistics

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

Classic code:

char chr[1001]

int s[10]={0},num=0;

for(int i=0;chr[i]!='\0';i++)

{

        num=chr[i]-'0';//The place to pay attention

        s[num]++;

}

output:

for(int j=0;j<10;j++)

{

    if(s[j]==0)

        continue;

        printf("%d:%d\n",j,s[j]);

}

Source code:

#include<stdio.h>
int main()
{
  char chr [ 1000 ] = { 0 };
  int a [ 10 ] = { 0 }, num = 0 ;
  scanf("%s",chr);
  for(int i=0;chr[i]!='\0';i++)
  {
    num=chr[i]-'0';
    a[num]++;
    }
    for(int j=0;j<10;j++)
    {
    if(a[j]==0)continue;
    printf("%d:%d\n",j,a[j]);
    }
    return 0;
}

Guess you like

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