【題解】ZeroJudge d899: NOIP2010 1.数字统计

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=d899

#include <iostream>
using namespace std;

int main() {
    int L, R, tmp, ans;
    while (cin >> L >> R) {
        ans = 0;
        for (; L<=R; ++L) {
            tmp = L;
            while (tmp > 0) {
                ans += (tmp % 10 == 2);
                tmp /= 10;
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Python code (credit: Amy Chou)

while True:
    try:
        L, R = map(int, input().split())
        #print(L, R)
        ans = 0
        for i in range(L, R+1):
            tmp = i
            while (tmp > 0):
                ans += (tmp % 10 == 2)
                tmp //= 10

        print(ans)
    except:
        break
分享本文 Share with friends