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;
    }
    

      

  • 相关阅读:
    C 语言中字符的输入输出
    C 语言 ctype.h 中系列字符处理函数
    C 语言中 for 循环的几种用法
    C 中优先级和关系运算符
    字符串和格式化输入/输出 [printf & scanf]
    C++中关于string类的一些API总结
    两大基本数据类型
    这些时候的总结
    PL/SQL 十进制数转任意进制
    复现题目[CISCN 2019 华东北赛区 Web2 WriteUp](https://www.zhaoj.in/read-6100.html)的一些东西
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4828118.html
Copyright © 2011-2022 走看看