IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    蓝桥杯 2019第十届蓝桥杯B组C++ 特别数的和

    Debug客栈发表于 2019-04-01 21:49:21
    love 0
    Featured image of post 蓝桥杯 2019第十届蓝桥杯B组C++ 特别数的和

    问题描述

    小明对数位中含有2、0、1、9 的数字很感兴趣(不包括前导0),在1 到 40 中这样的数包括1、2、9、10 至32、39 和40,共28 个,他们的和是574。 请问,在1 到n 中,所有这样的数的和是多少?

    输入格式

    输入一行包含两个整数n。

    输出格式

    输出一行,包含一个整数,表示满足条件的数的和。

    样例输入

    40

    样例输出

    574

    评测用例规模与约定

    对于20% 的评测用例,1<= n <= 10。 对于50% 的评测用例,1<= n <=100。 对于80% 的评测用例,1<= n <=1000。 对于所有评测用例, 1<=n <=10000。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    
    bool check(int n)
    {
     while(n)
     {
     int t=n%10;
     if(t==2||t==0||t==1||t==9)
     return true;
     n/=10;
     }
     return false;
    }
    
    int main()
    {
     int n,ans=0;
     cin>>n;
     for(int i=1;i<=n;i++)
     {
     if(check(i))
     ans+=i;
     }
     cout<<ans<<endl;
     return 0;
    }
    


沪ICP备19023445号-2号
友情链接