POJ 1564 Sum It Up dfs

Source: Internet
Author: User

Question: give you a sum, then give you a n, then give you n, and then find some numbers from n, so that the sum of these numbers is n. The number of n given is decreasing, so that the output result is also output in the form of decreasing.
Idea: Because n is at most 12, it is obvious that all situations can be searched brute force, and then the sum can be determined. If sum is used, the output can be used. Note the following two points: first, there is a deduplication process. My processing is to store all the appropriate values in a vector, then find the appropriate de-vector to judge whether it is repeated with a specific one by one. If they are not repeated, it will match the meaning of the question and record this to the vector. Because the final output is from large to small, I used a struct to save the answer, and finally sorted the struct in the lower order. Then, the positive solution is obtained.
Code:
[Cpp]
# Include <iostream>
# Include <string. h>
# Include <cstdio>
# Include <algorithm>
# Include <vector>
Using namespace std;
 
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
Struct add {
Int ans [15];
Int size;
} Aa [1, 100005];
Int num [15], flag [15], num2 [15], sum, n, numvv, numaa;
Bool isok;
Vector <int> vv [5005];
Bool fun (int cnt ){
Bool is = false;
For (int I = 0; I <numvv; ++ I ){
Is = false;
If (vv [I]. size () = cnt ){
For (int j = 0; j <vv [I]. size (); ++ j ){
If (num2 [j]! = Vv [I] [j]) {
Is = true;
Break;
}
}
If (! Is)
Return false;
}
}
Return true;
}
Void dfs (int begin, int id, int cnt ){
If (id = cnt ){
Int ss = 0;
For (int I = 0; I <cnt; ++ I)
Ss + = num2 [I];
If (ss = sum & fun (cnt )){
Isok = true;
Aa [numaa]. size = cnt;
For (int I = 0; I <cnt; ++ I ){
Aa [numaa]. ans [I] = num2 [I];
}
Numaa ++;
For (int I = 0; I <cnt; ++ I ){
Vv [numvv]. push_back (num2 [I]);
}
Numvv ++;
}
Return;
}
For (int I = begin; I <n; ++ I ){
If (! Flag [I]) {
Num2 [id] = num [I];
Flag [I] = true;
Dfs (I, id + 1, cnt );
Flag [I] = false;
}
}
}
Bool cmp (add a, add B ){
For (int I = 0; I <13; ++ I ){
If (a. ans [I]> B. ans [I])
Return true;
Else if (a. ans [I] <B. ans [I])
Return false;
}
}
Int main (){
// Freopen ("1.txt"," r ", stdin );
While (scanf ("% d", & sum, & n ){
CLR (num, 0 );
CLR (flag, 0 );
CLR (vv, 0 );
Numvv = 0;
Numaa = 0;
For (int I = 0; I <n; ++ I)
Scanf ("% d", & num [I]);
Isok = false;
Printf ("Sums of % d: \ n", sum );
For (int I = 1; I <= n; ++ I ){
CLR (flag, 0 );
CLR (num2, 0 );
Dfs (0, 0, I );
}
If (! Isok)
Printf ("NONE \ n ");
Else {www.2cto.com
Sort (aa, aa + numaa, cmp );
For (int I = 0; I <numaa; ++ I ){
Int len = aa [I]. size;
For (int j = 0; j <aa [I]. size-1; ++ j ){
Printf ("% d +", aa [I]. ans [j]);
}
Printf ("% d \ n", aa [I]. ans [len-1]);
}
}
}
Return 0;
}

Author: wmn_wmn

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.