Input N numbers and output all possible permutation and combination
A line of code and a line of tears... The hands are burned in hot laptops .... More than six hours ago... Finally, laruence ....
Yesterday, I asked a question about the arrangement and combination. The original question is to enter four numbers, for example, 1 2 3 4, and output all possible arrangement and combination.
It should not be difficult to say brute force. Code + debug, half an hour.
What if we enter N numbers?
Let's start with a simple brute-force method. If you enter four numbers, output all the permutation and combination.
The code is short and simple, so the tables of numerical constants and other values are concerned ....
/**********************************************************************code writer : EOFcode date :2014.06.11e-mail : jasonleaster@gmail.comcode purpose: just for fun...************************************************************************/#include
void fun(void);int main(){ fun(); return 0;}void fun(void){ int array[4]; int buffer[4]; int temp = 0; int circle_1 = 0; int circle_2 = 0; int circle_3 = 0; int circle_4 = 0; printf("Hey,guys! Please input four numbers\n"); //Input four number . for(temp = 0;temp < 4;temp++) { while(!scanf("%d",&array[temp])) for(temp = 0;temp < 4;temp++) { while(!scanf("%d",&array[temp])) { while(getchar() != '\n') { } printf("Input again and make sure the input data is right.\n"); } } for(circle_1 = 0;circle_1 < 4;circle_1++) { buffer[0] = array[circle_1]; for(circle_2 = 0;circle_2 < 4;circle_2++) { if(array[circle_2] != array[circle_1]) { buffer[1] = array[circle_2]; for(circle_3 = 0;circle_3 < 4;circle_3++) { if(array[circle_3] != array[circle_2] && array[circle_3] != array[circle_1]) { buffer[2] = array[circle_3]; for(circle_4 = 0;circle_4 < 4;circle_4++) { if(array[circle_4] != array[circle_3] && array[circle_4] != array[circle_2] && array[circle_4] != array[circle_1]) { buffer[3] = array[circle_4]; for(temp = 0;temp < 4;temp++) { printf("%d ",buffer[temp]); } printf("\n"); } } } } } } }}
It can be seen that the nested for loop layers depend on the number of data waiting for arrangement, and the overhead is quite large.
If N numbers are input and all permutation and combination are output, this method is not feasible.
Test results:
Ubuntu2 @ ubuntu :~ /Desktop $./a. out
Hey, guys! Please input four numbers
1 2 3 4
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
Recursion .... Make n versions.
The comments have not been written yet. Make up the comments tomorrow and go to the demo first.
/****************************************************************code writer : EOFcode date : 2014.06.12e-mail:jasonleaster@gmail.comcode purpose: It's a hard time to finished this program but everthing isOk...It was here. Just change the macro ARRAYSIZE into the number of inputvariable.This program would process it and print out all combinationof your input.****************************************************************/#include
#define ARRAYSIZE 3#define USED 1#define UNUSED 0struct location{ int state[ARRAYSIZE];};int buffer[ARRAYSIZE];int array[ARRAYSIZE];int check(int depth,struct location current_state);void fun(void);int main(){ fun(); return 0;}void fun(void){ int temp = 0; int buffer[ARRAYSIZE]; struct location initial_state; for(temp = 0;temp < ARRAYSIZE;temp++) { while(!scanf("%d",&array[temp])) { while(getchar() != '\n') { } printf("Input error\nPlease input agina!"); } } for(temp = 0;temp < ARRAYSIZE;temp++) { buffer[temp] = 0; initial_state.state[temp] = UNUSED; } printf("\n\n"); check(ARRAYSIZE-1,initial_state);}int check(int depth,struct location current_state){ int temp = 0; int foo = 0; int last_location = -1; if(depth > 0) { for(foo = 0;foo < ARRAYSIZE ;foo++) { for(temp = 0;temp < ARRAYSIZE ;temp++) { if(temp <= last_location) { continue; } if(current_state.state[temp] == UNUSED) { current_state.state[temp] = USED; last_location = temp; buffer[ARRAYSIZE-(depth+1)] = array[temp]; check(depth-1,current_state); current_state.state[last_location] = UNUSED;//clear used location and prepare for next depth. break; } } } } else if(depth == 0) { for(temp = 0;temp < ARRAYSIZE ;temp++) { if(current_state.state[temp] == UNUSED) { current_state.state[temp] = USED; last_location = temp; buffer[ARRAYSIZE-(depth+1)] = array[temp]; break; } } for(temp = 0;temp < ARRAYSIZE;temp++) { printf("%d ",buffer[temp]); } printf("\n"); }}