题目链接->link
思路
- 字符串数组存储整数,用整形数组存储0-9位累计的数目。字符ascii码0-9转换位数字需要x-‘0’,同理大写字符转小写:A-‘A’+'a’或者直接A+‘32’。
代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
using namespace std;
int main(){
char str[1005];
cin>>str;
int ans[10]={0};
for(int i=0;i<strlen(str);i++){//累计0-9对应有多少个
ans[str[i]-'0']++;
}
for(int i=0;i<10;i++){
if(ans[i]){
printf("%d:%d\n",i,ans[i]);
}
}
return 0;
}