题解 | #[NOIP2013]记数问题#
[NOIP2013]记数问题
http://www.nowcoder.com/practice/28b2d9f2bf2c48de94a1297ed90e1732
试计算在区间1 到n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次? 例如,在1到11 中,即在1、2、3、4、5、6、7、8、9、10、11 中,数字1 出现了4 次。
#include<stdio.h>
int main()
{
int n, x;
scanf("%d %d", &n, &x);
int i;
int count = 0;
for (i = 1; i <= n; i++)
{
int m = i;
while (m)
{
if (m % 10 == x)
count++;
m /= 10;
}
}
printf("%d\n", count);
}
腾讯成长空间 5958人发布
