【NOIP2010普及组】数字统计

Description

请统计某个给定范围[L, R]的所有整数中,数字 2 出现的次数。比如给定范围[2, 22],数字 2 在数 2 中出现了 1 次,在数 12 中出现 1 次,在数 20 中出现 1 次,在数 21 中出现 1 次,在数 22 中出现 2 次,所以数字 2 在该范围内一共出现了 6次。

Input

输入共 1 行,为两个正整数 L 和 R,之间用一个空格隔开。

Output

输出共 1 行,表示数字 2 出现的次数。

Sample Input 1 

2 22 

Sample Output 1

6 

Sample Input 2 

2 100 

Sample Output 2

20 

Hint

1 ≤ L ≤ R≤ 10000。

Source

NOIP2010普及组

Language: 
C++
Theme: 
Solarized Light
1
#include <iostream>
2
#include <string>
3
using namespace std;
4
int main()
5
{
6
    int g,s,b,q,w,c=0;
7
    int i,j;
8
    int n,m;
9
    cin>>n>>m;
10
    for(i=n;i<=m;i++)
11
    {
12
        g=i%10;
13
        s=i/10%10;
14
        b=i/100%10;
15
        q=i/1000%10;
16
        w=i/10000%10;
17
     
18
        if(g==2)c++;
19
        if(s==2)c++;
20
        if(b==2)c++;
21
        if(q==2)c++;
22
        if(w==2)c++;
23
    }
24
    cout<<c;
25
}

posted @ 2018-05-01 11:16  SMALLff  阅读(486)  评论(0编辑  收藏  举报