洛谷 P1980【计数问题】 题解(2)

还有一种办法,就是用stringstream函数将每一次的数全都转化成char一维数组样式的字符串,然后逐位扫一遍即可。

(记得判断字符时将规定数字+48)


 

 1 //Stand up for the faith!
 2 #include<bits/stdc++.h> 
 3 #define LL long long
 4 #define FRO freopen
 5 #define FCO fclose
 6 #define US unsigned
 7 #define RI register
 8 #define CN const
 9 #define MAXN 20001
10 #define INF 0x3f3f3f3f
11 
12 using namespace std;
13 
14 inline LL read()
15 {
16     LL s=0,w=1;
17     char ch=getchar();
18     while(ch<'0'||ch>'9')
19     {
20         if(ch=='-')w=-1;
21         ch=getchar();
22     }
23     while(ch>='0'&&ch<='9')
24     {
25         s=s*10+ch-'0';
26         ch=getchar();    
27     }
28     return s*w;
29 }
30 
31 char c[1313];
32 LL n,m,ans=0;
33 
34 int main(void)
35 {
36     n=read();
37     m=read();
38     m+=48;
39     for(int i=1;i<=n;i++)
40     {
41         stringstream ss;
42         ss<<i;
43         ss>>c;
44         for(int j=0;j<strlen(c);j++)
45         {
46             if(c[j]==m) ans+=1;
47         }
48     }
49     printf("%lld",ans);
50 
51 return 0;
52 }

 



 

posted @ 2019-03-22 21:49  KGB1331  阅读(149)  评论(0编辑  收藏  举报