• hdu1258Sum It Up (DFS)


    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

    知识点:DFS

    题意:给个数t,再给一组数列,降序组合出题目给的数t,要求按降序排列输出.

    难点:判断重复的组合。

     1 #include<cstdlib>
     2 #include<cstdio>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<cstring>
     6 using namespace std;
     7 int vis[20];
     8 int  a[20];
     9 int b[20];
    10 int t,n,flag;
    11 void dfs(int p,int num,int cnt)
    12 {
    13     if(num==t)
    14     {
    15         flag=1;
    16         for(int j=0;j<cnt;j++)
    17         printf(j==cnt-1?"%d
    ":"%d+",b[j]);
    18         return;
    19     }
    20     if(num>t)
    21         return;
    22     if(cnt>=n)
    23         return;
    24     int temp=-1;
    25     for(int i=p;i<n;i++)
    26     {
    27         if(!vis[i]&&temp!=a[i])//判断重复:手动模拟一遍便知。
    28         {
    29             vis[i]=1;
    30             b[cnt]=a[i];
    31             temp=a[i];
    32             dfs(i,num+a[i],cnt+1);
    33             vis[i]=0;
    34         }
    35     }
    36 }
    37 int main()
    38 {
    39     while(~scanf("%d%d",&t,&n))
    40     {
    41         if(t==0&&n==0)
    42         break;
    43         memset(a,0,sizeof(a));
    44         for(int i=0;i<n;i++)
    45             scanf("%d",&a[i]);
    46         sort(a,a+n);
    47         reverse(a,a+n);//reverse(a,a+n)代表对a数组中所有元素反序存储
    48         printf("Sums of %d:
    ",t);
    49         memset(vis,0,sizeof(vis));
    50         flag=0;
    51         dfs(0,0,0);
    52         if(flag==0)
    53             printf("NONE
    ");
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    OpenJ_Bailian
    Codeforces Global Round 2: D.Frets On Fire
    Gym 102035 NWU2019组队赛2.0
    Gym 102035H :Zuhair and the Dag
    Gym 102035E:New Max
    Gym
    Linux常用命令-总结
    Linux常用命令
    Linux常用命令
    分布式会话 拦截器 单点登录
  • 原文地址:https://www.cnblogs.com/ZP-Better/p/4639611.html
Copyright © 2020-2023  润新知