/* Implement the Backtracking Method */
/* Junyi sun at CCNU */
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Define n 100
Int n_stack [N], R, M, N;
Void p_stack (){
Int I;
Printf ("{");
For (I = 0; I <R-1; I ++)
Printf ("% d,", n_stack [I]);
If (n_stack [0]! = M-n + 1) printf ("% d},", n_stack [I]);
Else printf ("% d}", n_stack [I]);
}
Void Gen (){
R = 0;
N_stack [R ++] = 1;
While (1 ){
While (r <n)
N_stack [r ++] = n_stack [r-1] + 1;
P_stack ();
While (n_stack [r-1] <m ){
N_stack [r-1] ++;
P_stack ();
}
While (n_stack [r-1] = m-n + r & r! = 0) r --;
If (r = 0) return;
N_stack [r-1] ++;
}
}
Int main (){
Printf ("Enter the base number:/n ");
Scanf ("% d", & m );
For (n = 1; n <= m; n ++ ){
Printf ("C (% d, % d): {", n, m );
Gen ();
Printf ("}/n ");
}
Return 0;
}
---------------------------------
Test results:
Enter the base number:
1
C (1, 1): {1 }}
Enter the base number:
2
C (1, 2): {1}, {2 }}
C (2, 2): {1, 2 }}
Enter the base number:
3
C (1, 3): {1}, {2}, {3 }}
C (2, 3): {1, 2}, {1, 3}, {2, 3 }}
C (3, 3): {1, 2, 3 }}
Enter the base number:
4
C (): {1}, {2}, {3}, {4 }}
C (2, 4): {1, 2}, {1, 3}, {1, 4}, {2, 4}, {3, 4 }}
C (3,4): {1, 2, 3}, {1, 2, 4}, {1, 3}, {2, 3, 4 }}
C }}
Enter the base number:
5
C (): {1}, {2}, {3}, {4}, {5 }}
C (): {}, {}, {2, 3}, {2, 4}, {}, {3, 4 }, {3, 5 },{ 4, 5 }}
C (): {, 3}, {, 4}, {, 5}, {, 4}, {, 5, 5}, {2, 3, 4}, {2, 3, 5}, {2, 4}, {3, 4, 5 }}
C }}
C (5, 5): {1, 2, 3, 4, 5 }}