洛谷P1980 计数问题

题目描述

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

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

输入输出格式

输入格式:

输入文件名为 count.in。

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

输出格式:

输出文件名为 count.out。

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

输入输出样例

输入样例#1:

11 1


输出样例#1:

4


说明

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


WriteUp:


参考AC代码:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

int main(void)
{
	int n,x;
	int temp;
	int count = 0;
	scanf("%d%d",&n,&x);
	for (int i=1;i<=n;i++)
	{
		temp = i;
		while (temp)
		{
			if (temp%10 == x)
			{
				count++;
			}
			temp /= 10;
		}
	}
	printf("%d",count);
	return 0;
}


原文地址:https://www.cnblogs.com/csnd/p/12897081.html