B2082 题解

· · 题解

此题模拟即可。

将在 [L,R] 范围的数挨个审查,通过取模和除法分离这些数的各个数位。最后判断这些数中有没有 2

#include<bits/stdc++.h>
using namespace std;
int L,R,c,d,ans,i;
int main()
{
    cin>>L>>R;
    for(i=L;i<=R;i++)
    {
        c=i;
        while(c!=0)
        {
            d=c%10;
            c=c/10;
            if(d==2)
                ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}