题解:P1179 [NOIP2010 普及组] 数字统计
cgy20140502
·
·
题解
本题考察对一个数进行拆分,模版见下:
while(n!=0){
n/=10;
}
```cpp
while(n!=0){
if(n%10==2) s++;
n/=10;
}
```
得出之后加入循环,再区间中找 $2$ 。
## AC代码:
```cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b,s=0,n;
cin>>a>>b;
for(int y=a;y<=b;y++)//区间循环
{
n=y;
while(n!=0)
{
if(n%10==2) s++;//进行对2的判断
n/=10;
}
}
cout<<s;
return 0;//优美结束
}
```