zoukankan      html  css  js  c++  java
  • POJ 2248 Addition Chains

    Addition Chains
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4480   Accepted: 2454   Special Judge

    Description

    An addition chain for n is an integer sequence <a0, a1,a2,...,am="">with the following four properties: 
    • a0 = 1 
    • am = n 
    • a0 < a1 < a2 < ... < am-1 < am 
    • For each k (1<=k<=m) there exist two (not necessarily different) integers i and j (0<=i, j<=k-1) with ak=ai+aj

    You are given an integer n. Your job is to construct an addition chain for n with minimal length. If there is more than one such sequence, any one is acceptable. 
    For example, <1,2,3,5> and <1,2,4,5> are both valid solutions when you are asked for an addition chain for 5.

    Input

    The input will contain one or more test cases. Each test case consists of one line containing one integer n (1<=n<=100). Input is terminated by a value of zero (0) for n.

    Output

    For each test case, print one line containing the required integer sequence. Separate the numbers by one blank. 
    Hint: The problem is a little time-critical, so use proper break conditions where necessary to reduce the search space. 

    Sample Input

    5
    7
    12
    15
    77
    0
    

    Sample Output

    1 2 4 5
    1 2 4 6 7
    1 2 4 8 12
    1 2 4 5 10 15
    1 2 4 8 9 17 34 68 77
    题目大意:输入一个数字n,输出一个数列,要求数列的第一个为1,后面的由前面的数字是前面数字中人选两个想家的和,可以选两个相同的数字,亦即对于任意i和j (0<=i, j<=k-1) 有 ak=ai+aj,输出长度最短的一个序列。
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int MinLen;
    int n;
    int result[10005] = {1, 2};
    int temp[10005] = {1, 2};
    
    void DFS(int nLen, int num)
    {
        if (nLen >= MinLen || num > n)
        {
            return;
        }
        if (num == n)
        {
            if (MinLen > nLen)
            {
                MinLen = nLen;
                for (int i = 2; i < nLen; i++)
                {
                    result[i] = temp[i];
                }
            }
            return;
        }
        for (int i = nLen - 1; i >= 0; i--)
        {
            temp[nLen] = temp[i] + temp[nLen - 1];
            DFS(nLen + 1, temp[nLen]);
        }
    }
    
    int main()
    {
        while(scanf("%d", &n) != EOF && n != 0)
        {
            if (n == 1)
            {
                printf("1
    ");
                continue;
            }
            MinLen = 0x7fffffff;
            DFS(2, 2);
            printf("%d", result[0]);
            for (int i = 1; i < MinLen; i++)
            {
                printf(" %d", result[i]);
            }
            printf("
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    CentOS 7.3报Centos7_Base库缺少GPG公钥
    nginx重写(隐藏)index.php目录
    工作经历简写
    Centos7.4安装htop
    nginx 超时时间配置说明
    c#中数据从数据库到客户端主要几种的导出方式(导出到excel,导出到word)
    C#操作word文档如何设置表格的行高
    Windows计划任务定时启动程序运行时发生错误的解决办法
    Asp.Net MVC中请求json文件时返回404 not found的解决办法
    Angularjs 如何自定义Img的ng-load 事件
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3240952.html
Copyright © 2011-2022 走看看