♐ CSP_J 组真题
2010年
1. 数字统计

线上OJ:

一本通:http://ybt.ssoier.cn:8088/problem_show.php?pid=1949 (opens in a new tab)
Acwing:https://www.acwing.com/problem/content/443/ (opens in a new tab)

相似题目:2013NOIP普及组真题 1. 计数问题 (opens in a new tab)

本题与之一样,由于数据范围不大,直接暴力即可

暴力
#include <bits/stdc++.h>
using namespace std;
 
int l, r, ans = 0;
 
int main()
{
    scanf("%d %d", &l, &r);
    for(int i = l; i <= r; i++)
    {
        int k = i;
        while(k)
        {
            if( k%10 == 2 ) ans++;
            k /= 10;
        }
    }
    printf("%d", ans);
    return 0;
}