链接:https://www.nowcoder.com/acm/contest/70/B
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
定义一个数字为幸运数字当且仅当它的所有数位都是4或者7。
比如说,47、744、4都是幸运数字而5、17、467都不是。
定义next(x)为大于等于x的第一个幸运数字。给定l,r,请求出next(l) + next(l + 1) + ... + next(r - 1) + next(r)。
比如说,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
简单的二叉树结构,把幸运数字按顺序存入数组。
0
4 7
44 47 74 77
444 447 474 477 744 747 774 777
#include <iostream>
#include <cstdio>
#include <cstring>
typedef long long ll;
using namespace std;
ll a[1050]={0};
int main()
{
ll k=0,s=0,l,r,p=1;
for(int i=1;i<=1024;i++)
{
int t=i/2+1,q=i%2?4:7;
a[i]=10*a[i-t]+q;
}
cin>>l>>r;
for(int i=0;i<=1024;i++)
{
if(a[i]>=l)
{
for(;l<=a[i];l++)
{
s+=a[i];
if(l==r)break;
}
if(l==r)break;
}
}
cout<<s<<endl;
return 0;
}
本文介绍了一种算法,用于计算给定区间内所有幸运数字的累加和。幸运数字定义为仅由数字4和7组成的正整数。通过构建一个包含所有幸运数字的数组,并遍历指定范围内的幸运数字来实现求和。
523

被折叠的 条评论
为什么被折叠?



