牛客提高D2t2 幸运数字考试

分析

预处理出所有合法数字

然后直接lower_bound查询即可

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define int long long
char s[50];
int n,m,t,cnt,d[100100];
inline void dfs(int sum4,int sum7,int len,int wh){
    if(sum4==len&&sum7==len){
      d[++cnt]=wh;
      return;
    }
    if(sum4<len)dfs(sum4+1,sum7,len,wh*10+4);
    if(sum7<len)dfs(sum4,sum7+1,len,wh*10+7);
    return;
}
signed main(){
    int i,j,k;
    for(i=1;i<=9;i++)dfs(0,0,i,0);
    scanf("%lld",&t);
    while(t--){
      scanf("%lld",&n);
      if(n>d[cnt]){
          puts("44444444447777777777");
      }else {
          k=lower_bound(d+1,d+cnt+1,n)-d;
          printf("%lld\n",d[k]);
      }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yzxverygood/p/11405704.html