NUC1399 Sum It Up【DFS】

Sum It Up

时间限制: 1000ms 内存限制: 65535KB

通过次数: 1总提交次数: 1

问题描述
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.
输入描述
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 x 1 , . . . , x n . 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 x 1 , . . . , x n 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.
输出描述
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 distinct; the same sum cannot appear twice.
样例输入
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
样例输出
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
来源
Mid-Central USA 1997



问题分析:(略)

这个问题和《HDU1258 POJ1564 UVA574 UVALive5319 ZOJ1711 Sum It Up【DFS】 》是同一个问题,代码直接用就AC了。

程序说明:参见参考链接。

参考链接:HDU1258 POJ1564 UVA574 UVALive5319 ZOJ1711 Sum It Up【DFS】

题记:程序做多了,不定哪天遇见似曾相识的。

AC的C++程序如下:

/* HDU1258 Sum It Up */  
  
#include <stdio.h>  
#include <memory.h>  
  
#define MAXN 15  
  
int data[MAXN];  
int kcount[MAXN];  
int dsum[MAXN];  
int ans[MAXN];  
int kind;  
int residue;  
int count;  
  
void print_result()  
{  
    int i, j, k;  
    for(i=0, j=0; i<kind; i++) {  
        for(k=1; k<=ans[i]; k++) {  
            if(j == 0)  
                printf("%d", data[i]);  
            else  
                printf("+%d", data[i]);  
            j++;  
        }  
    }  
    printf("
");  
}  
  
void dfs(int k)  
{  
    int i;  
  
    if(k == kind || dsum[k] < residue)  
        return;  
  
    for(i=kcount[k]; i>=0; i--) {  
        residue -= i * data[k];  
        if(residue < 0) {  
            ;  
        } else if(residue == 0) {  
            ans[k] = i;  
            count++;  
            print_result();  
            ans[k] = 0;  
        }  else if(residue > 0 && residue >= data[kind-1]) {  
            ans[k] = i;  
            dfs(k+1);  
            ans[k] = 0;  
        }  
        residue += i * data[k];  
    }  
}  
  
int main(void)  
{  
    int total, n, sum, i;  
  
    while(scanf("%d%d", &total, &n) != EOF) {  
        // 判定结束条件  
        if(total == 0 && n == 0)  
            break;  
  
        // 读入数据,并求和  
        sum = 0;  
        for(i=0; i<n; i++) {  
            scanf("%d", &data[i]);  
            sum += data[i];  
        }  
  
        // 整理:同值合并  
        for(i=1, kind=0, kcount[0]=1; i<n; i++) {  
            if(data[i] == data[kind])  
                kcount[kind]++;  
            else {  
                data[++kind] = data[i];  
                kcount[kind] = 1;  
            }  
        }  
        kind++;  
  
        // 计算总和:dsum[i]为第i类之后各个数的总和  
        dsum[0] = sum;  
        for(i=1; i<kind; i++)  
            dsum[i] = dsum[i-1] - data[i-1] * kcount[i-1];  
  
        // 输出第一行  
        printf("Sums of %d:
", total);  
  
        // 深度优先搜索,并输出结果  
        count = 0;  
        residue = total;  
        memset(ans, 0, sizeof(ans));  
        dfs(0);  
  
        // 输出结果  
        if(count == 0)  
            printf("NONE
");  
    }  
  
    return 0;  
}



原文地址:https://www.cnblogs.com/tigerisland/p/7563656.html