poj 1564 Sum It Up

poj 1564 Sum It Up,第1张

poj 1564 Sum It Up
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>using namespace std;const int maxnum = 1001, maxn = 12;int t, n, sum[maxnum], number[maxn][2], answer[maxn], total;bool found;void init(){    int i, x;    memset(sum, 0, sizeof(sum));    memset(answer, 0, sizeof(answer));    found = false;    total = 0;    for (i = 0; i < n; i++)    {        cin >> x;        sum[x]++;    }    for (i = 1000; i >= 1; i--)        if (sum[i] > 0)        { number[total][0] = i; number[total][1] = sum[i]; total++;        }}void print(){    int i, j;    bool first = true;    if (!found)    {        found = true;        cout << "Sums of " << t << ":n";    }    for (i = 0; i < n; i++)        for (j = 1; j <= answer[i]; j++) if (first) {     cout << number[i][0];     first = false; } else {     cout << "+" << number[i][0]; }    cout << endl;}void make(int temp, int space){    int i;    if (space == 0)    {        print();        return;    }    if (temp == total)        return;    for (i = number[temp][1]; i >= 0; i--)        if (space - i * number[temp][0] >= 0)        { answer[temp] = i; make(temp + 1, space - i * number[temp][0]);        }    answer[temp] = 0;}int main(){    while (cin >> t >> n, n | t)    {        init();        make(0, t);        if (!found)        { cout << "Sums of " << t << ":n"; cout << "NONEn";        }    }    return 0;}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4899711.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存