我的CPU(脑袋)40.5度

P1179 [NOIP2010 普及组] 数字统计

@[douyachong](/user/1080331) 显然你要枚举每一位: ```cpp int j=i; while(j){ if(j%10==2)sum++; j/=10; } ``` 即可
by FiraCode @ 2024-02-12 12:05:00


@[FiraCode](/user/528430) 嘶~~~~~ 啊,我知道了,不懂!
by douyachong @ 2024-02-12 12:07:45


而且应该是 i <= r,因为是闭区间
by NPH_Zhao @ 2024-02-12 12:20:54


>$1 ≤ L ≤R≤ 100000$。 而你,只算了个位和十位,是不够的,所以每一位都要枚举一遍 ```cpp #include<bits/stdc++.h> using namespace std; int l,r,sum=0; int main(){ cin>>l>>r; for(int i=l;i<=r;i++){//注意,需要枚举到r int j=i; while(j){ if(j%10==2)sum++;//看个位是否是2 j/=10;//把十位变成个位,百位变成十位……清除个位 } } cout<<sum; cout<<endl; return 0; } ```
by xiangzhenze611 @ 2024-02-12 12:24:19


@[douyachong](/user/1080331) 你这个好像只枚举了个位和十位呀~显然每一位都要枚举的
by wisdom2010 @ 2024-02-17 16:24:32


@[douyachong](/user/1080331) ```cpp #include<bits/stdc++.h> using namespace std; int f(int a) { int cnt=0; while(a>0) { if(a%10==2) cnt++; a/=10; } return cnt; } int main() { int l,r; cin>>l>>r; int sum=0; for(int i=l;i<=r;i++) sum+=f(i); cout<<sum; return 0; } ```
by wisdom2010 @ 2024-02-17 16:25:33


@[douyachong](/user/1080331) 这样就可以了(求关注qwq)
by wisdom2010 @ 2024-02-17 16:26:16


你的for更没有有什么区别,要两个for呀! 一个枚举数字,另一个枚举数位 像这样 ```cpp for(int i=n;i<=m;i++) { for(int j=i;j>0;j/=10) { if(j%10==2) o++; } } cout<<o; ``` ~~别想抄代码~~ ```cpp #include <bits/stdc++.h> using namespace std; int main() { int n,m,o=0; cin>>n>>m; for(int i=n;i<=m;i++) { for(int j=i;j>0;j/=10) { if(j%10==2) o++; } } cout<<o; } ```
by wyxrl @ 2024-02-20 01:11:55


@[wyxrl](/user/1266960) 不听不听,我就抄~~~~
by douyachong @ 2024-02-24 10:02:02


|