Http://poj.org/problem? Id = 1564
Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 1711.
Simple Deep Search, mainly this sentence if (! Vis [I] & Data [I] <= sum & (data [I]! = Data [I-1] | I = POS )).
Ensure that two consecutive identical numbers only have one chance to enter ans [], avoiding repeated results.
Code:
# Include <cstdio>
# Include <cstring>
Int data [15];
Int ans [15];
Bool vis [15];
Bool flag;
Int N;
Void DFS (INT sum, int count, int POS ){
If (! Sum ){
Flag = true;
Printf ("% d", ANS [0]);
For (INT I = 1; I <count; I ++)
Printf ("+ % d", ANS [I]);
Printf ("\ n ");
Return;
}
For (INT I = Pos; I <n; I ++ ){
If (! Vis [I] & Data [I] <= sum & (data [I]! = Data [I-1] | I = POS )){
Vis [I] = true;
Ans [count] = data [I];
DFS (Sum-data [I], Count + 1, I + 1 );
Vis [I] = false;
}
}
}
Int main (){
Int T, I, J;
While (~ Scanf ("% d", & T, & N) & T + n ){
Memset (VIS, false, sizeof (VIS ));
For (I = 0; I <n; I ++ ){
Scanf ("% d", & Data [I]);
If (data [I]> T) vis [I] = true;
}
Printf ("sums of % d: \ n", t );
Flag = false;
DFS (T, 0, 0 );
If (! Flag) printf ("None \ n ");
}
Return 0 ;}