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;
    }
     
  • 相关阅读:
    2020去奔跑
    2020前两个月学习心得
    2020年1_2月寒假学习心得
    寒假中期考核个人总结
    Numpy、Matplotlib和pandas学习路线引导
    Python学习路线引导-Python入门基础
    2019在路上
    2020/11/28助教一周小结(第九周)
    2020/11/3助教一周小结(第五周)
    2020/10/25助教一周小结(第四周)
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3240952.html
Copyright © 2011-2022 走看看