简单题
思路:组成数字的种类只有[0,1,2,…,9] 共10个,如果使用字符串接收实例数字,遍历每位后映射到一个一维数组,数组值为统计变量;最后按格式输出即可;
代码如下,提交使用g++
using namespace std;
int main()
{
string str;
int count[10], len;
cin >> str;
len = str.length();
memset(count, 0, sizeof(count));
for(int i=0; i<len; i++)
{
count[str[i]-'0']++;
}
for(int i=0; i<=9; i++)
{
if(count[i])
printf("%d:%d\n", i, count[i]);
}
return 0;
}
本文介绍了一种使用C++实现的简单方法,通过遍历输入的字符串并映射到一维数组来统计每个数字字符出现的次数。这种方法适用于快速统计0到9之间的数字频率。
2924

被折叠的 条评论
为什么被折叠?



