神奇数字二
题目:
https://ac.nowcoder.com/acm/problem/15291
题目描述
定义一个数字为幸运数字当且仅当它的所有数位都是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)。
输出描述:
一个数字表示答案。
//next为大于等于x的第一个幸运数字 #include<bits/stdc++.h> using namespace std; typedef long long ll; int cnt=0; const int maxn=1e6+10; ll a[maxn]; void dfs(ll x) { if(x>1e10) return; a[cnt++]=x; dfs(x*10+4); dfs(x*10+7); } int main() { int l,r; cin>>l>>r; dfs(0); sort(a,a+cnt); ll sum=0; ll ant=0; for(ll i=l;i<=r;i++) { while(i>a[ant]) ant++; sum+=a[ant]; } cout<<sum; }
浙公网安备 33010602011771号