L1-003. Single digit statistics

Given a k-bit 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 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, which is a positive integer N with 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
Output sample:
0:2
1:3
3:1
Note: If the input number is relatively small, you can directly take the operation of %10, /10 to write, but because the number is relatively large, you should use String entry numbers.

#include<stdio.h>
#include<string.h>
int main()
{
    int i, t, len;
    char s[1001];//定义一个不超过1000位的正整数N
    int count[10] = {0};//数组赋值0
    scanf("%s",  s);//字符串的输入不需要&
    len = strlen(s);//读取字符串的长度
    for(i=0; i<len; i++){
        t=s[i]-'0';//0在ASCII码中的值是48,所以也可以用48代替'0'
        count[t]++;
    }
    for(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=325584519&siteId=291194637