带注释 P1980 计数问题

it2024-10-11  43

题目描述 试计算在区间 1到 n 的所有整数中,数字 x(0 ≤ x ≤ 9)x(0≤x≤9)共出现了多少次?例如,在 1到 11中,即在 1,2,3,4,5,6,7,8,9,10,111,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 4 次。

输入格式 2个整数n,x之间用一个空格隔开。

输出格式 1个整数,表示x出现的次数。

输入输出样例 输入 #1 复制

11 1

输出 #1 复制

4

说明/提示 对于 100%100%的数据,1≤ n ≤ 1,000,000,0 ≤ x ≤ 91≤n≤1,000,000,0≤x≤9。

#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int main (){ int n,x; scanf ("%d %d",&n,&x); int i,j,k,g,t,cnt; for (t = 1,cnt = 0;t <= n;t++){ i=t; while (i){ g=i%10; if (g == x) cnt++; i/=10; } } printf ("%d",cnt); return 0; }
最新回复(0)