【NOIP2010】【Luogu1179】数字统计(模拟,多位数分离)

problem

  • 请统计某个给定范围 [L, R] 的所有整数中,数字 2 出现的次数。
  • 例如2~22中2出现了6次

solution

  • 枚举L~R
  • 对于i分离它的每一位,判断是否为2,累加答案。

codes

#include<iostream>
using namespace std;
int main(){
    int L, R, ans = 0;
    cin>>L>>R;
    for(int i = L; i <= R; i++){
        int t = i;
        while(t > 0){
            ans += t%10==2;
            t /= 10;
        }
    }
    cout<<ans<<'\n';
    return 0;
}
posted @ 2018-07-07 14:16  gwj1139177410  阅读(135)  评论(0编辑  收藏  举报
选择