zoukankan      html  css  js  c++  java
  • Sum It Up ( dfs ➕ 去重)

    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 <algorithm>
     3 #include <stdlib.h>
     4 #include <string>
     5 #include <string.h>
     6 #include <set>
     7 #include <queue>
     8 #include <math.h>
     9 #include <stdbool.h>
    10 
    11 #define LL long long
    12 #define inf 0x3f3f3f3f
    13 using namespace std;
    14 const int MAXN=1000005;
    15 
    16 int n,m;
    17 int id;
    18 int flag = 0;
    19 int vis[105];
    20 int a[105],b[105];
    21 
    22 bool cmp(int x,int y)
    23 {
    24     return x>y;
    25 }
    26 
    27 
    28 void dfs(int sum,int start)
    29 {
    30     if (sum > n)
    31         return ;
    32     if (sum == n)
    33     {
    34         flag = 1;
    35         cout << b[0];
    36         for (int i=1;i<id;i++)
    37            printf("+%d",b[i]);
    38         printf("
    ");
    39         return ;
    40     }
    41     for (int i=start;i<m;i++)
    42     {
    43         if (!vis[i])
    44         {
    45             vis[i] = 1;
    46             b[id++] = a[i];
    47             dfs(sum+a[i],i+1);
    48             vis[i] = 0;
    49             id--;
    50             while(i+1<m&&a[i]==a[i+1])//搜索完毕后,若下一个搜索的数仍与当前相同,则跳过直至不相同
    51                 i++;
    52         }
    53     }
    54 }
    55 
    56 
    57 
    58 
    59 int main()
    60 {
    61     //freopen("../in.txt","r",stdin);
    62     while (~scanf("%d%d",&n,&m))
    63     {
    64         flag = 0;
    65         memset(vis,0, sizeof(vis));
    66        // memset(b,0, sizeof(b));
    67         id = 0;
    68         if (n== 0 && m==0 )
    69             break;
    70         for (int i=0;i<m;i++)
    71             cin >> a[i];
    72         sort(a,a+m,cmp);
    73         printf("Sums of %d:
    ",n);
    74         dfs(0,0);
    75         if (flag == 0)
    76             printf("NONE
    ");
    77     }
    78     return 0;
    79 }
  • 相关阅读:
    comparator接口与Comparable接口的区别
    heap和stack有什么区别
    聚集索引和非聚集索引(整理)
    SQL里的EXISTS与in、not exists与not in
    SQL中CONVERT转化函数的用法
    GCC 对C语言的扩展
    C++宏定义详解
    How to Find Processlist Thread id in gdb !!!!!GDB 使用
    Netdata----Linux 性能实时监测工具
    java开发C语言编译器
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11222364.html
Copyright © 2011-2022 走看看