zoukankan      html  css  js  c++  java
  • UVA-1374 Power Calculus (迭代加深搜索)

    题目大意:问最少经过几次乘除法可以使x变成xn

    题目分析:迭代加深搜索。

    代码如下:

    # include<iostream>
    # include<cstdio>
    # include<set>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    int num[20],maxn,cnt;
    bool dfs(int d,int maxd,int n)
    {
        if(d==maxd)
            return num[cnt-1]==n;
        if((num[cnt-1]<<(maxd-d))<n)
            return false;
        for(int i=0;i<cnt;++i){
            num[cnt]=num[cnt-1]+num[i];
            ++cnt;
            if(num[cnt-1]<=1000&&dfs(d+1,maxd,n))
                return true;
            --cnt;
            num[cnt]=num[cnt-1]-num[i];
            ++cnt;
            if(num[cnt-1]>0&&dfs(d+1,maxd,n))
                return true;
            --cnt;
        }
        return false;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)&&n)
        {
            for(int maxd=0;;++maxd){
                cnt=0;
                num[cnt++]=1;
                if(dfs(0,maxd,n)){
                    printf("%d
    ",maxd);
                    break;
                }
            }
        }
        return 0;
    }
    

      

    打表版本:

    # include<iostream>
    # include<cstdio>
    # include<set>
    # include<cstring>
    # include<algorithm>
    using namespace std;
    int ans[1000]={0,1,2,2,3,3,4,3,4,4,5,4,5,5,5,4,5,5,6,5,6,6,6,5,6,6,6,6,7,6,6,5,6,6,
    7,6,7,7,7,6,7,7,7,7,7,7,7,6,7,7,7,7,8,7,8,7,8,8,8,7,8,7,7,6,7,7,8,7,8,8,8,7,8,8,8,8,
    8,8,8,7,8,8,8,8,8,8,9,8,9,8,9,8,8,8,8,7,8,8,8,8,9,8,9,8,9,9,9,8,9,9,9,8,9,9,9,9,9,9,9,8,
    9,9,9,8,9,8,8,7,8,8,9,8,9,9,9,8,9,9,9,9,9,9,9,8,9,9,9,9,9,9,10,9,9,9,9,9,9,9,9,
    8,9,9,9,9,9,9,10,9,10,9,10,9,10,10,10,9,10,10,10,9,10,10,10,9,10,9,10,9,9,9,9,8,
    9,9,9,9,10,9,10,9,10,10,10,9,10,10,10,9,10,10,10,10,10,10,10,9,10,10,10,10,10,10,
    10,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,9,10,10,10,10,10,10,10,9,10,10,
    10,9,10,9,9,8,9,9,10,9,10,10,10,9,10,10,11,10,11,10,10,9,10,10,11,10,11,10,10,10,
    10,10,10,10,10,10,10,9,10,10,10,10,10,10,11,10,10,10,11,10,11,11,11,10,11,10,11,10,
    11,10,11,10,11,10,10,10,10,10,10,9,10,10,10,10,10,10,11,10,11,10,11,10,11,11,11,10,11,11,11,
    10,11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,
    10,11,11,11,10,11,11,11,10,11,10,11,10,10,10,10,9,10,10,10,10,11,10,11,10,11,11,11,10,11,
    11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,11,
    11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,10,11,11,11,11,11,
    11,11,11,11,11,11,11,12,11,12,11,11,11,12,11,12,11,11,11,11,11,11,11,11,11,11,10,11,11,
    11,11,11,11,11,11,11,11,12,11,12,11,11,10,11,11,12,11,12,11,11,10,11,11,11,10,11,10,10,9,10,
    10,11,10,11,11,11,10,11,11,12,11,12,11,11,10,11,11,12,11,12,12,11,11,12,12,12,11,12,11,11,10,
    11,11,12,11,12,12,12,11,11,12,12,11,12,11,12,11,11,11,12,11,12,11,11,11,12,11,11,11,11,11,11,10,11,
    11,11,11,11,11,12,11,11,11,12,11,12,12,12,11,12,11,12,11,12,12,12,11,12,12,12,12,12,
    12,12,11,12,12,12,11,12,12,12,11,12,12,12,11,12,12,12,11,12,12,12,11,12,11,12,11,
    12,11,11,11,11,11,11,10,11,11,11,11,11,11,12,11,12,11,12,11,12,12,
    12,11,12,12,12,11,12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,11,12,12,
    12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,
    12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
    12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,11,12,12,12,11,12,
    11,12,11,11,11,11,10,11,11,11,11,12,11,12,11,12,12,12,11,12,12,12,11,12,12,12,12,
    12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,13,12,12,12,
    12,11,12,12,12,12,13,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,
    12,12,12,12,12,13,12,12,12,13,12,13,12,12,12,13,12,12,12,12,12,12,11,12,12,12,12,12,
    12,13,12,12,12,13,12,13,12,12,12,13,12,13,12,13,12,12,12,12,12,12,12,12,12,
    12,11,12,12,12,12,12,12,12,12,12,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,
    13,13,12,13,13,13,12,13,12,13,12,13,13,13,12,13,13,13,12,13,12,13,12,12,12,13,12,
    13,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,13,12,13,12,
    12,12,12,12,13,12,13,13,13,12,13,13,13,12,13,12,12,11,12,12,13,12,13,13,13,12};
    int main()
    {
        int n;
        while(scanf("%d",&n)&&n)
            printf("%d
    ",ans[n-1]);
        return 0;
    }
    

      

  • 相关阅读:
    第二十三讲:访问者模式
    第二十二讲:备忘录模式
    第二十讲:迭代模式
    第十九讲:职责链模式
    第十八讲:中介者模式
    UTC时间、GMT时间、本地时间、Unix时间戳
    【基础】SQL Server系统库的作用
    【基础】SQL Server系统库的作用
    【基础】SQL Server系统库的作用
    【收集】C#一些基础的面试题
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4828118.html
Copyright © 2011-2022 走看看