• 1126 数字统计 2010年NOIP全国联赛普及组


    1126 数字统计

    2010年NOIP全国联赛普及组

    时间限制: 1 s
    空间限制: 128000 KB
    题目等级 : 白银 Silver
     
     
     
     
    题目描述 Description

    请统计某个给定范围[L, R]的所有整数中,数字2出现的次数。

    比如给定范围[2, 22],数字2在数2中出现了1次,在数12中出现1次,在数20中出现1次,在数21中出现1次,在数22中出现2次,所以数字2在该范围内一共出现了6次。

    数据范围 1 ≤ L ≤ R≤ 10000。

    输入描述 Input Description

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

    输出描述 Output Description

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

    样例输入 Sample Input

    2 22

    样例输出 Sample Output

    6

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<string>
     5 #include<algorithm>
     6 using namespace std;
     7 int l,r;
     8 char s[10];
     9 int ans=0;
    10 int main()
    11 {
    12     cin>>l>>r;
    13     for(int i=l;i<=r;i++)
    14      {
    15          int q=i;
    16          /*sprintf(s,"%d",i);
    17          int len=strlen(s);
    18          for(int j=0;j<l;j++)
    19           {
    20               if(s[j]=='2')
    21                {
    22                    ans++;
    23                }
    24           }*/
    25           while(q!=0)
    26            {
    27                int qq=q%10;
    28                if(qq==2)
    29                 {
    30                     ans++;
    31                 }
    32                 q/=10;
    33            }
    34      }
    35      cout<<ans;
    36      return 0;
    37 }
  • 相关阅读:
    7、猜年龄
    6、continue语句
    5、break语句
    4、while循环练习
    poj 2378
    poj 2342
    poj 2287
    poj 2228
    poj 1191
    srm 578 dv2 1000pt
  • 原文地址:https://www.cnblogs.com/lyqlyq/p/6735929.html
Copyright © 2020-2023  润新知