幸运数字 2

来源点击打开链接

链接:https://www.nowcoder.com/acm/contest/70/B
来源:牛客网

题目描述

定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。
比如说,47、744、4都是幸运数字而5、17、467都不是。
定义next(x)为大于等于x的第一个幸运数字。给定l,r,请求出next(l) + next(l + 1) + ... + next(r - 1) + next(r)。

输入描述:

两个整数l和r (1 <= l <= r <= 1000,000,000)。

输出描述:

一个数字表示答案。
示例1

输入

2 7

输出

33
示例2

输入

7 7

输出

7

简单的二叉树结构,把幸运数字按顺序存入数组。

[cpp] view plain copy
 
  1.             0  
  2.      4           7  
  3. 44      47     74     77  
  4.  447 474 477 744 747 774 777  
[cpp] view plain copy
 
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <cstring>  
  4. typedef long long ll;  
  5. using namespace std;  
  6. ll a[1050]={0};  
  7. int main()  
  8. {  
  9.     ll k=0,s=0,l,r,p=1;  
  10.     for(int i=1;i<=1024;i++)  
  11.     {  
  12.         int t=i/2+1,q=i%2?4:7;  
  13.         a[i]=10*a[i-t]+q;  
  14.     }  
  15.     cin>>l>>r;  
  16.     for(int i=0;i<=1024;i++)  
  17.     {  
  18.         if(a[i]>=l)  
  19.         {  
  20.             for(;l<=a[i];l++)  
  21.             {  
  22.                 s+=a[i];  
  23.                 if(l==r)break;  
  24.             }  
  25.             if(l==r)break;  
  26.         }  
  27.     }  
  28.     cout<<s<<endl;  
  29.     return 0;  
  30. }  
原文地址:https://www.cnblogs.com/Nlifea/p/11746074.html