zoukankan      html  css  js  c++  java
  • Sum It Up HDU

    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,14,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. 

    InputThe 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. 
    OutputFor 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. 
    Sample Input

    4 6 4 3 2 2 1 1
    5 3 2 1 1
    400 12 50 50 50 50 50 50 25 25 25 25 25 25
    0 0

    Sample Output

    Sums of 4:
    4
    3+1
    2+2
    2+1+1
    Sums of 5:
    NONE
    Sums of 400:
    50+50+50+50+50+50+25+25+25+25
    50+50+50+50+50+25+25+25+25+25+25
    DFS
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 #include <cstdio>
    14 #include <cstdlib>
    15 using namespace std;
    16 int a[20];
    17 int sum,n;
    18 int b[20];
    19 int c[20];
    20 int add;
    21 int cmp(int a,int b)
    22 {
    23     return a>b;
    24 }
    25 void dfs(int x,int p,int sum1,int k)
    26 {
    27     int i;
    28     if(sum1>sum)
    29         return;
    30     if(sum1==sum)
    31     {
    32         int flag=1;
    33         add++;
    34         for(int i=0;i<k;i++)
    35         {
    36             if(flag)
    37                 {
    38                     flag =0;
    39                     cout<<b[i];
    40                 }
    41             else
    42                 cout<<"+"<<b[i];
    43         }
    44         cout<<endl;
    45     }
    46     for(i=p;i<n;i++)
    47     {
    48         b[k]=a[i];
    49         dfs(a[i],i+1,sum1+a[i],k+1);
    50         while(i+1<n&&a[i]==a[i+1])
    51             i++;
    52     }
    53 }
    54 int main()
    55 {
    56     while(cin>>sum>>n)
    57     {
    58         if(n==0)
    59             break;
    60             memset(a,0,sizeof(a));
    61             memset(b,0,sizeof(b));
    62             memset(c,0,sizeof(c));
    63         for(int i=0;i<n;i++)
    64             cin>>a[i];
    65             sort(a,a+n,cmp);
    66             add=0;
    67         cout<<"Sums of "<<sum<<":"<<endl;
    68         dfs(0,0,0,0);
    69         if(add==0)
    70             cout<<"NONE"<<endl;
    71     }
    72     return 0;
    73 }
    View Code
  • 相关阅读:
    c++常用库
    boost
    android
    UITableView 多选
    c++ 比较两个集合
    事件加不上的另一种原因
    ios多线程
    ubuntu android
    jna StdCallCallback 回调问题查证
    java
  • 原文地址:https://www.cnblogs.com/dulute/p/7272735.html
Copyright © 2011-2022 走看看