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;
    }
  • 相关阅读:
    02-27 朴素贝叶斯
    JQuery UI datepicker 使用方法(转)
    纯CSS兑现侧边栏/分栏高度自动相等(转)
    在触屏设备上面利用html5裁剪图片(转)
    简单几步让CentOS系统时间同步(转)
    百度与谷歌地图坐标转换代码(转)
    jQuery插件开发全解析(转)
    Centos 安装ImageMagick 与 imagick for php步骤详解
    将windows目录共享到linux
    Event事件的兼容性(转)
  • 原文地址:https://www.cnblogs.com/hdyss/p/10808776.html
Copyright © 2011-2022 走看看