zoukankan      html  css  js  c++  java
  • hdoj

    Problem Description
    Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
     
    Input
    The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12(inclusive), and x1,...,xn will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
     
    Output
    For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distince; the same sum connot appear twice.
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace::std;
    
    int map[15],t,n;
    int map_[15],k=0;
    int sum;
    
    void dfs1(int l,int ans)
    {
        if(sum > t)
        {
            return ;
        }
        if( sum == t)
        {
            for(int i =0;i<ans;i++)
            {
                if(i)
                {
                    printf("+%d",map_[i]);
                }
                else
                {
                    printf("%d",map_[i]);
                }
            }
            cout<<endl;
            k++;
        }
        for(int i = l;i<n;i++)
        {
            if(map[i] == map[i-1] && i>l)       //这一个语句是重点
                continue;
            sum = sum + map[i];
            map_[ans] = map[i];
            dfs1(i+1,ans+1);
            sum =sum - map[i];
            map_[ans] = 0;
        }
    }
    int main()
    {
        while(scanf("%d %d",&t,&n) && n != 0)
        {
            memset(map,0,sizeof(map));         //重置地图和标记
            memset(map_,0,sizeof(map_));
            k=0;
            for(int i=0; i<n;i++)
            {
                scanf("%d",&map[i]);
            }
            printf("Sums of %d:
    ",t);
            dfs1(0,0);
            if(k == 0)
            {
                printf("NONE
    ");
            }
        }
    
        return 0;
    }
  • 相关阅读:
    吕滔博客 --------MYSQL 备份与参数详解
    solaris知识库
    F5 负载均衡
    日志管理 rsyslog服务浅析
    你所不知到的C++ 系列
    php内核探索
    shell 编程中使用到得if语句内判断参数
    linux查看CPU性能及工作状态的指令
    MYSQL 5.7 主从复制 -----GTID说明与限制 原创
    C#:Json数据反序列化为Dictionary并根据关键字获取指定的值
  • 原文地址:https://www.cnblogs.com/hdyss/p/10808776.html
Copyright © 2011-2022 走看看