Ultraviolet A 110-Meta-Loopless Sorts

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.