https://ac.nowcoder.com/acm/contest/19306/1002

来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 131072K,其他语言262144K

64bit IO Format: %lld

题目描述

试计算在区间1 到n 的所有整数中,数字x(0 ≤ x ≤ 9)共出现了多少次?

例如,在1到11 中,即在1、2、3、4、5、6、7、8、9、10、11 中,数字1 出现了4 次。

输入描述:

输入共1行,包含2个整数n、x,之间用一个空格隔开。

输出描述:

输出共1行,包含一个整数,表示x出现的次数。

示例1

输入

11 1

输出

4

备注:

对于100%的数据,1≤ n ≤ 1,000,000,0 ≤ x ≤ 9。

#include

using namespace std;

int main()

{

int n,num;
cin >> n >> num;
int i,count=0;
for(i=0;i<=n;i++)
{
    int term=i;
    while(term>0)
    {
        if(term%10==num)  count++;
        term/=10;
    }
}
cout << count;

}