top1编程
← 返回题目
题解

数字统计

1 条题解

  • 0
    @ 2026-7-29 0:21:31
    #include <iostream>
    using namespace std;
    
    int countDigitTwo(int n) {
        int count = 0;
        while (n > 0) {
            if (n % 10 == 2)
                count++;
            n /= 10;
        }
        return count;
    }
    
    int main() {
        int L, R;
        cin >> L >> R;
    
        int total = 0;
        for (int i = L; i <= R; i++) {
            total += countDigitTwo(i);
        }
    
        cout << total << endl;
    
        return 0;
    }
    
    • 1