Prepare some questions about the principle of rejection. The principle of Rejection requires any combination of a number factor. For example, 30 = 2*3*5, you need to obtain (2), (3), (5), (2, 3), (2, 5), (3, 5), (2, 3, 5) These combinations can be implemented using dfs. Write one and leave it as a template.
Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <string. h>
Using namespace std;
# Define CLR (arr, val) memset (arr, val, sizeof (arr ))
Int num [4] = {2, 3, 5, 7 };
Int linnum [4];
Int flag [4];
Void dfs (int id, int begin, int cnt ){
If (id = cnt ){
For (int I = 0; I <4; ++ I)
Printf ("% d", linnum [I]);
Printf ("\ n ");
Return;
} Www.2cto.com
For (int I = begin; I <4; ++ I ){
If (! Flag [I]) {
Flag [I] = true;
Linnum [id] = num [I];
Dfs (id + 1, I, cnt );
Flag [I] = false;
}
}
}
Int main (){
For (int I = 1; I <= 4; ++ I ){
CLR (flag, 0 );
CLR (linnum, 0 );
Dfs (0, 0, I );
Printf ("ss \ n ");
}
Return 0;
}
Author: wmn_wmn