解题
2026-10-07 10:18:50
发布于:北京
0阅读
0回复
0点赞
思路:
从l到r都遍历一遍,由于要统计2出现的次数,所以可以用字符串,再从字符串的第一位看到最后一位,如果是2就数量加一。最终输出统计出的数量。
```cpp
#include<bits/stdc++.h>
using namespace std;
int main(){
int l,r,cnt=0;
cin>>l>>r;
for(int i=l;i<=r;i++){
string s=to_string(i);
for(int j=0;j<s.size();j++){
if(s[j]=='2'){
cnt++;
}
}
}
cout<<cnt;
return 0;
}
这里空空如也

有帮助,赞一个