日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

zcmu1133(dfs+判重)

發(fā)布時間:2025/3/15 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zcmu1133(dfs+判重) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1133: 第九章:致我們終將逝去的青春

Time Limit:?1 Sec??Memory Limit:?128 MB
Submit:?28??Solved:?18
[Submit][Status][Web Board]

Description

??????????????????????

/青 春 是 用 來 追 憶 的

/當 你 懷 揣 著 她 時

/她 一 文 不 值

/只 有 將 她 耗 盡 后

/再 回 過 頭 看

/一 切 才 有 了 意 義

/愛 過 我 們 的 人 和 傷 害 過 我 們 的 人

/都 是 我 們 青 春 存 在 的 意 義

?????????

?

在ACM中找尋?青春的意義:

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 distinct; the same sum cannot 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

題意:給你一個數(shù),求在給出的m個數(shù)中選幾個加起來的和可以等于n,輸出這幾種等式,注意等式不能一樣。

解析:dfs+判重,直接的dfs我們可能出現(xiàn)有重復的答案,比如400的例子,直接dfs有兩個一樣的答案,所以我們直接跳過50,跳到25。

#include<bits/stdc++.h> using namespace std;#define e exp(1) #define pi acos(-1) #define mod 1000000007 #define inf 0x3f3f3f3f #define ll long long #define ull unsigned long long #define mem(a,b) memset(a,b,sizeof(a)) int gcd(int a,int b){return b?gcd(b,a%b):a;}const int maxn=1000+5; int n,m,a[maxn],ans[maxn],flag; void dfs(int deep,int sum,int length) {if(sum>n)return ;if(sum==n){flag=1;for(int i=0; i<length; i++){if(i==0)printf("%d",ans[i]);else printf("+%d",ans[i]);}puts("");}for(int i=deep; i<m; i++){ans[length]=a[i];dfs(i+1,sum+a[i],length+1);while(a[i]==a[i+1])i++;} } int main() {while(~scanf("%d%d",&n,&m)){if(n==0&&m==0)break;for(int i=0; i<m; i++)scanf("%d",&a[i]);flag=0;printf("Sums of %d:\n",n);dfs(0,0,0);if(!flag)puts("NONE");}return 0; }

?

總結

以上是生活随笔為你收集整理的zcmu1133(dfs+判重)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。