C-Language experiment--permutation Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ The title describes 4 distinct numbers, sequentially outputting an arrangement of three distinct digits. Enter a 4 integer. Output all permutations, the output order is shown in the sample. Sample input
1 2 3 4
Sample output
1 2 31 3 22 1 32 3 13 1 23 2 11 2 41 4 22 1 42 4 14 1 24 2 11 3 41 4 33 1 43 4 14 1 34 3 12 3 42 4 33 2 43 4 24 2 34 3 2
Perhaps it is very ridiculous, freshman should learn the topic, I was to do now, but at least I have not given up and forget!
Mr Yu said: "There are two kinds of creatures climbing the pyramid, one is the eagle, by their own talent can fly very high, ascended the peak of the pyramid is easy."
There is a kind of creature is a snail, relying on a perseverance and confidence, it can climb to the peak of the pyramid, as long as it is still working, as long as it has not given up, a
There's still hope in the cut!
I hope everyone in the road to success will be like a snail to continue to persevere! One day the pyramid will be reached, to see people that others cannot see.
Scenery.
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < Limits.h> #include <algorithm>using namespace std;int a[4];void print_permutation (int n, int *a, int *c, int cur) {int I, J; if (cur==n) {for (i=0; i<n; i++) {if (i==0) printf ("%d", a[i]); else printf ("%d", a[i]); } printf ("\ n"); } else {for (i=0; i<3; i++) {int ok=1; for (j=0; j<cur; J + +) {if (a[j]==c[i]) ok=0; } if (OK) {a[cur]=c[i]; Print_permutation (n, A, C, cur+1); }}}}int Main () {int i, J; for (i=0; i<4; i++) scanf ("%d", &a[i]); int b[3]; int c[3]; B[0]=A[0]; B[1]=A[1]; B[2]=A[2]; 1 2 3 for (i=0; i<3; i++) c[i]=b[i]; Print_permutation (3, B, c, 0); B[0]=A[0]; b[1]=a[1]; B[2]=A[3]; 1 2 4 for (i=0; i<3; i++) c[i]=b[i]; Print_permutation (3, B, c, 0); B[0]=A[0]; B[1]=A[2]; B[2]=A[3]; 1 3 4 for (i=0; i<3; i++) c[i]=b[i]; Print_permutation (3, B, c, 0); B[0]=A[1]; B[1]=A[2]; B[2]=A[3]; 2 3 4 for (i=0; i<3; i++) c[i]=b[i]; Print_permutation (3, B, c,0); return 0;}
Sdut OJ 1163 C language Experiment--permutation (originally did not recursive generation arrangement, this topic now only then complements the Rujia to give the wording * "the template" of course has generated the entire arrangement function existence)