Uva 110 Meta-Loopless Sorts using a program to write a program is a bit complicated to backtrack questions

Source: Internet
Author: User
Tags sorts

This is an interesting question that requires you to write a pascal program that is directly sorted by comparison.

I think the range of the question data is 8. I thought it was cheap and I used a switch to output the data.

Then we found that the number of comparisons is a factorial level, and the 8 factorial is quite large. I'm afraid it will not be handed in.

So we use the Backtracking Method.

In fact, he compares the numbers one by one and inserts them in front of them to continue tracing.

The processing of else is troublesome. After a long time, it is finally the same as the standard answer.

If the indentation is not processed, submit it to the ac. It seems that the oj has not checked the indentation. If there is a check, it will have to be processed.

Code: (no indentation is performed)

 

# Include <cstdio> const int maxn = 10; int n, arr [maxn]; void insert_sort (int p, int c) {// insert sort for (int I = c; i> p; I --) arr [I] = arr [I-1]; arr [p] = c;} int dfs (int d) {int tmp [d + 1]; // create an array to store the original value. Otherwise, the for (int j = 1; j <= n; j ++) tmp [j] = arr [j]; for (int I = d; I> = 1; I --) {// cyclically run dfs printf ("if % c <% c then \ n", arr [I] + 'a'-1, d + 'A'); insert_sort (I + 1, d + 1); // Insert the next letter Enter the position I + 1 if (d + 1 = n) {// dfs to the deepest position, output printf ("writeln ("); printf ("% c ", arr [1] + 'a'-1); for (int j = 2; j <= d + 1; j ++) printf (", % c ", arr [j] + 'a'-1); printf (") \ n"); printf ("else \ n");} else {dfs (d + 1 ); printf ("else \ n") ;}for (int j = 1; j <= n; j ++) // restore the array arr [j] = tmp [j];} insert_sort (1, d + 1); // The following is the last case where letters are inserted before the entire array, here there is no else if (d + 1 = n) {printf ("writeln ("); printf ("% c", arr [1] + 'a'-1); for (int j = 2; j <= d + 1; j ++) printf (", % c ", arr [j] + 'a'-1); printf (") \ n");} else dfs (d + 1); for (int I = 1; I <= n; I ++) arr [I] = tmp [I];} int main () {int t; scanf ("% d", & t ); while (t --) {scanf ("% d", & n); // the previous part of printf ("program sort (input, output); \ nvar \ n "); printf ("a"); for (int I = 2; I <= n; I ++) printf (", % c", I + 'a'-1 ); printf (": integer; \ nbegin \ nreadln ("); printf ("A"); for (int I = 2; I <= n; I ++) printf (", % c", I + 'a'-1 ); printf ("); \ n"); dfs (0); // start Deep Search printf ("end. \ n "); if (t! = 0) printf ("\ n");} return 0 ;}# include <cstdio> const int maxn = 10; int n, arr [maxn]; void insert_sort (int p, int c) {// insert sort for (int I = c; I> p; I --) arr [I] = arr [I-1]; arr [p] = c;} int dfs (int d) {int tmp [d + 1]; // create an array to store the original value, otherwise, the for (int j = 1; j <= n; j ++) tmp [j] = arr [j]; for (int I = d; i> = 1; I --) {// cyclically perform dfsprintf ("if % c <% c then \ n ", arr [I] + 'a'-1, d + 'A'); insert_sort (I + 1, d + 1); // Insert the next letter to the position I + 1 if (d + 1 = n) {// dfs to the deepest position, output printf ("writeln ("); printf ("% c", arr [1] + 'a'-1); for (int j = 2; j <= d + 1; j ++) printf (", % c", arr [j] + 'a'-1); printf (") \ n "); printf ("else \ n");} else {dfs (d + 1); printf ("else \ n");} for (int j = 1; j <= n; j ++) // restores the array arr [j] = tmp [j];} insert_sort (1, d + 1 ); // The following is the last case where letters are inserted before the entire array. Here there is no else if (d + 1 = n) {printf ("writeln ("); printf ("% c", arr [1] +' A'-1); for (int j = 2; j <= d + 1; j ++) printf (", % c ", arr [j] + 'a'-1); printf (") \ n");} elsedfs (d + 1); for (int I = 1; I <= n; I ++) arr [I] = tmp [I];} int main () {int t; scanf ("% d", & t ); while (t --) {scanf ("% d", & n); // the previous part of printf ("program sort (input, output); \ nvar \ n "); printf ("a"); for (int I = 2; I <= n; I ++) printf (", % c", I + 'a'-1 ); printf (": integer; \ nbegin \ nreadln ("); printf ("a"); for (int I = 2; I <= n; I ++) printf (", % c", I + 'a'-1); printf ("); \ n"); dfs (0 ); // start Deep Search printf ("end. \ n "); if (t! = 0) printf ("\ n");} return 0 ;}

 

Related Article

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.