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

    http://acm.hdu.edu.cn/showproblem.php?pid=1258

    Sum It Up

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4564    Accepted Submission(s): 2349

    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.
     
    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
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int t,n,a[20],vis[20],b[20];
     6 int flag;
     7 void dfs(int x,int cur,int sum)
     8 {
     9     int i;
    10      if(sum==t)
    11      {
    12          flag=1;
    13          printf("%d",b[0]);
    14         for(i=1;i<cur;i++)
    15             printf("+%d",b[i]);
    16         printf("
    ");
    17         return ;
    18      }
    19     for(i=x;i<n;i++)
    20     {
    21         if(vis[i]==0&&sum<t)
    22           {
    23               vis[i]=1;
    24               b[cur]=a[i];
    25               dfs(i+1,cur+1,sum+a[i]);
    26                vis[i]=0;
    27              while(i+1<n&& a[i]==a[i+1])    //去除相同的分解式
    28                      i++;
    29           }
    30     }
    31 }
    32 int main()
    33 {
    34     int i;
    35     while(~scanf("%d%d",&t,&n))
    36     {
    37         if(t==0&&n==0)
    38               break;
    39           flag=0;
    40         for(i=0;i<n;i++)
    41            scanf("%d",&a[i]);
    42         printf("Sums of %d:
    ",t);
    43          dfs(0,0,0);
    44         //printf("flag=%d
    ",flag);
    45         if(flag==0)
    46            printf("NONE
    ");
    47      }
    48     return 0;
    49 
    50 }
  • 相关阅读:
    WF 学习笔记 (1) 浅谈 WF 和 MVC 架构
    从HelloWorld看iphone程序的生命周期
    android开发我的新浪微博客户端阅读微博UI篇(6.1)
    关于微博服务端API的OAuth认证实现
    iphone开发我的新浪微博客户端开篇
    一个完整的新浪微博客户端android版OAuth认证示例
    android开发我的新浪微博客户端OAuth认证过程中用WebView代替原来的系统自带浏览器
    iphone开发我的新浪微博客户端用户登录准备篇(1.1)
    android开发我的新浪微博客户端登录页面功能篇(4.2)
    android开发我的新浪微博客户端登录页面UI篇(4.1)
  • 原文地址:https://www.cnblogs.com/cancangood/p/4385389.html
Copyright © 2011-2022 走看看