试计算在区间 1到 n 的所有整数中,数字 x(0≤x≤9) 共出现了多少次?例如,在 1 到 11 中,即在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 次。
输入格式
2个整数 n , x ,之间用一个空格隔开。
输出格式
1 个整数,表示 x 出现的次数。
数据范围
对于
100%的数据,1≤n≤1,000,000, 0≤x≤9。
Sample Input
11 1
Sample Output
4
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,x,i,j,s=0;
cin>>n>>x;
//把个位,十位,百位数字拿出来与X比较,相等就进行计数
for(i=1;i<=n;i++){//1到n
j=i;//对每一个数进行判断
while(j>0){
if(j%10==x){//对10取余;
s++;
}
j=j/10;//对10取整
}
}
cout<<s;
return 0;
}