B. Tavas and SaDDas

题目链接:http://codeforces.com/problemset/problem/535/B

47 Lucky数字问题

比赛时用的是打表的代码,居然过不了!真不知道为什么,先记录下吧,以后可能会知道为什么。


DFS代码:

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#define LL long long
#define INF 0x7fffffff

//freopen("E:\\in.txt","r",stdin);

using namespace std;

int ans,n;

void dfs(int t,int k){
    if(t>=n) return;
    if(t<n) ans++;
    if(k<1000000000) dfs(k*4+t,k*10);
    if(k<1000000000) dfs(k*7+t,k*10);
}

int main(){
    while(cin>>n){
        ans=1;
        dfs(4,10);dfs(7,10);
        cout<<ans<<endl;
    }
    return 0;
}



打表代码:

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#define LL long long
#define INF 0x7fffffff

//freopen("E:\\in.txt","r",stdin);

using namespace std;

int ans[20000],tot;

void bfs(){
    queue<int> Q;
    Q.push(4);Q.push(7);
    while(!Q.empty()){
        int tp=Q.front();Q.pop();
        //if(tp<=100000000) cout<<tp<<endl;
        ans[tot++]=tp;
        int k=0;
        int p=tp;
        while(p) k++,p/=10;
        int kk=(int)pow(10,k);
        if(kk*4+tp<=1000000000 && kk*4+tp>0 && kk*4+tp>tp) Q.push(kk*4+tp);
        if(kk*7+tp<=1000000000 && kk*7+tp>0 && kk*7+tp>tp) Q.push(kk*7+tp);
    }
}

int main(){
    tot=0;
    bfs();
    sort(ans,ans+tot);
    //for(int i=0;i<(int)ans.size();i++) cout<<ans[i]<<" ";
    int N;
    while(cin>>N){
        for(int i=0;i<tot;i++)
            if(ans[i]==N) {
                cout<<(i+1)<<endl;
                break;
            }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值