幸运数字Ⅱ

幸运数字Ⅱ

https://ac.nowcoder.com/acm/problem/15291

dfs打表,记录1e9内的满足条件的幸运数;

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+10;
int cnt=0,rec[maxn];
void dfs(int x)
{
    if(x>444444444) return;
    rec[cnt++]=x*10+4;
    rec[cnt++]=x*10+7;
    dfs(x*10+4);
    dfs(x*10+7);
}
signed main()
{
    int l,r;
    cin>>l>>r;
    dfs(0);
    sort(rec,rec+cnt);
    int ans=0;
    int i=lower_bound(rec,rec+cnt,l)-rec;//找出左界
    int j=upper_bound(rec,rec+cnt,r)-rec;//右界
   for(int k=i;k<=j;k++)
   {
       ans=ans+(min(rec[k],r)-l+1)*rec[k];
       l=rec[k]+1;
   }
    cout<<ans;
}
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务