給定一個k位整數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),請編寫程序統計每種不同的個位數字出現的次數。例如:給定N = 100311,則有2個0,3個1,和1個3。
輸入格式:
每個輸入包含1個測試用例,即一個不超過1000位的正整數N。
輸出格式:
對N中每一種不同的個位數字,以D:M的格式在一行中輸出該位數字D及其在N中出現的次數M。要求按D的升序輸出。
輸入樣例:
100311
輸出樣例:
0:2
1:3
3:1
#include<stdio.h>
#include<string.h>
int main(){
char str[1010];
scanf("%s",str);//輸入數字
int len=strlen(str);//確定str長度
int count[10]={0};//計數出現的次數
for(int i=0;i<len;i++){
count[str[i] - '0']++;//str[1]=1,1的ascall碼等於49,'0'的ascall等於48,相減等於1
//count[]數組用來計數出現的次數
}
for(int i=0;i<10;i++){
if(count[i]!=0){
printf("%d:%d\n",i,count[i]);
}
}
return 0;
}