Problem : [NOIP2013普及组]计数问题
Time Limit: 1 Sec Memory Limit: 128 MB
Description
试计算在区间1到n的所有整数中,数字x(0≤x≤9)共出现了多少次?
例如,在1到11中,即在1、2、3、4、5、6、7、8、9、10、11中,数字1出现了4次。
Input
输入共1行,包含2个整数n、x,之间用一个空格隔开。
1≤n≤1,000,000,0≤x≤9
Output
输出共1行,包含一个整数,表示x出现的次数。
Sample Input
11 1
Sample Output
4
code:
#include <stdio.h>
int cal(int a,int x){
int ans;
ans=0;
while(a){
if(a%10==x)
ans++;
a/=10;
}
return ans;
}
int main(){
int n,x;
int i;
int ans;
scanf("%d%d",&n,&x);
ans=0;
for(i=1;i<=n;i++)
ans+=cal(i,x);
printf("%d
",ans);
return 0;
}