一本书的页码从自然数1开始顺序编码直到自然数n。书的页码按照通常的
习惯编排,每个页码都不含多余的前导数字0。例如第6页用6表示而不是
06或006。数字统计问题要求对给定书的总页码,计算出书的全部页码中
分别用到多少次数字0,1,2,3,.....9。
#include "stdio.h"
#include "string.h"
int main()
{
int i, t;
int n;
int count[10];
memset(count, 0, sizeof(count));
printf("请输入一个正整数:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
t = i;
while(t)
{
count[t%10]++;
t /= 10;
}
}
for(i=0; i<10; i++)
printf("数字%d出现的次数为%d\n", i, count[i]);
printf("\n");
return 0;
}
统计数字问题
最新推荐文章于 2023-01-15 19:29:18 发布