洛谷1980 计数问题

洛谷1980 计数问题

本题地址: http://www.luogu.org/problem/show?pid=1980

题目描述

试计算在区间 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。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<stack>
 6 #include<queue>
 7 #include<cstring>
 8 #define PAU putchar(' ')
 9 #define ENT putchar('
')
10 #define MSE(a,b) memset(a,b,sizeof(a))
11 #define REN(x) for(ted*e=fch[x];e;e=e->nxt)
12 #define TIL(x) for(int i=1;i<=x;i++)
13 #define ALL(x) for(int j=1;j<=x;j++)
14 using namespace std;
15 inline int read(){
16     int x=0;bool sig=true;char ch=getchar();
17     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=false;
18     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';return sig?x:-x;
19 }
20 inline void write(int x){
21     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
22     int len=0;static int buf[20];while(x)buf[len++]=x%10,x/=10;
23     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
24 }
25 int main(){
26     int n=read(),x=read();char s[20];int sum=0;
27     TIL(n){
28         sprintf(s,"%d",i);for(int j=0;s[j];j++)if(s[j]-'0'==x)sum++;
29     }write(sum);
30     return 0;
31 }
原文地址:https://www.cnblogs.com/chxer/p/4750194.html