The actual result is to find a full arrangement, because the maximum number given is 8, so there is a maximum of 8! .
Based on the given example, we can regard it as inserting a new element into the inverted structure of the given arrangement (in fact, it is inserting null). For example, if n = 3 is given, the first element is, insert B to AB, insert c to abc, and insert c to acb and cab in sequence, and return the first layer recursively. Then, B shifts forward to ba, subsequently, bac, bca, and CBA were generated one after another.
The Code is as follows:
# Include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> using namespace std; char a [10], B [10]; void print_space (int cur) // print space {for (int I = 0; I <cur; I ++) printf ("");} void judge (int cur, int n) {char d [10]; print_space (cur); if (cur = n) {printf ("writeln ("); printf ("% c ", a [0]); for (int I = 1; I <n; I ++) printf (", % c", a [I]); printf (") \ n "); return;} char c = B [cur]; a [cur] = B [cur]; a [cur + 1] = 0; strcpy (d, a); // Save the original string to d if (cur> 0) {int po; printf ("if"); printf ("% c <% c ", a [cur-1], a [cur]); printf ("then \ n"); judge (cur + 1, n); po = cur-1; for (int I = cur-1; I> 0; I --) {strcpy (a, d); // restore the original string, and then insert the element print_space (cur) in reverse order ); printf ("else if"); printf ("% c <% c", a [I-1], a [cur]); for (int j = cur; j> I; j --) a [j] = a [j-1]; a [I] = c; printf ("then \ n "); judge (cur + 1, n);} strcpy (a, d); for (int j = cur; j> 0; j --) a [j] = a [j-1]; a [0] = c; print_space (cur); printf ("else \ n"); judge (cur + 1, n);} else judge (cur + 1, n);} int main () {# ifdef test freopen ("sample.txt", "r", stdin ); # endif int t, n; scanf ("% d", & t); while (t --) {scanf ("% d", & n ); for (int I = 0; I <n; I ++) B [I] = 'A' + I; printf ("program sort (input, output ); \ nvar \ n "); printf (" % c ", B [0]); for (int I = 1; I <n; I ++) // initialization, assign it to a, B, c... printf (", % c", B [I]); printf (": integer; \ nbegin \ n"); printf ("readln (% c ", B [0]); for (int I = 1; I <n; I ++) printf (", % c", B [I]); printf ("); \ n "); judge (0, n); printf (" end. \ n "); if (t) puts (" ") ;}return 0 ;}