NOIP2013-普及组复赛-第一题-计数问题

题目描述 Description
试计算在区间 1 到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1
到 11 中,即在 1、2、3、4、5、6、7、8、9、10、11 中,数字 1 出现了 4 次。
 输入输出格式 Input/output
输入格式:
输入文件名为 count.in。 
输入共 1 行,包含 2 个整数 n、x,之间用一个空格隔开。
输出格式:
输出文件名为 count.out。 
输出共 1 行,包含一个整数,表示 x 出现的次数。
 输入输出样例 Sample input/output
样例测试点#1
输入样例:
11 1
输出样例:
4
说明 description
对于 100%的数据,1≤ n ≤ 1,000,000,0 ≤ x ≤ 9。
思路:这题入门难度,循环这个范围的数,对于每个数进行每位判断就可以了
代码如下:
 1 #include <stdio.h>
 2 int main()
 3 {
 4     int n,m,i,k;
 5     int ans=0;
 6     //freopen("count.in","r",stdin);
 7     //freopen("count.out","w",stdout);
 8     scanf("%d%d",&m,&n);
 9     for(i=1;i<=m;i++)
10     {
11         k=i;
12         while(k>0)
13         {
14             if(k%10==n) ans++;
15             k=k/10;
16         }
17     }
18     printf("%d
",ans);
19     return 0;
20 }
原文地址:https://www.cnblogs.com/geek-007/p/4662816.html