UVa meta-loopless Sorts (Recursive & code simulation & full arrangement)

Source: Internet
Author: User
Tags printf sort sorts time limit

110-meta-loopless Sorts

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem &problem=46

Background

Sorting holds a important place in the computer science. Analyzing and implementing various sorting algorithms forms a important part of the education of most computer scientists , and sorting accounts for a significant percentage to the world's computational resources. Sorting algorithms range from the bewilderingly popular Bubble sort, to Quicksort, to parallel sorting and sort ING networks. In this problem you are writing a program that creates a sorting program (a meta-sorter).

The Problem

The problem is to create several programs whose output are a standard Pascal program so sorts n numbers where n is the ly input to the program you'll write. The Pascal programs generated by your program must have the following properties:

They must begin with the program sort (input,output);

They must declare storage for exactly n integer variables. The names of the variables must come from the ' the ' of the ' letters ' of the alphabet (a,b,c,d,e,f).

A single READLN statement must read in values for all the integer variables.

Other than Writeln statements, the only statements in the program are if then else statements. The Boolean conditional for each If statement must consist of one strict inequality (either < or >) of two integer V Ariables. Exactly n! Writeln statements must appear in the program.

Exactly three semi-colons must appear in the programs 1.after of the program Header:program sort (input,output);

2.after the variable declaration:.: integer;

3.after the Readln statement:readln (...);

No redundant comparisons of the integer variables should be made. For example, during program execution, once it are determined that a < B, variables A and B should not be compared again .

Every Writeln statement must appear on a line by itself.

The programs must compile. Executing the program with input consisting ' any arrangement ' of any ndistinct integer values should the input Values being printed in sorted order.

For those unfamiliar and Pascal syntax, the example at the end of this problem completely defines the small subset of Pas Cal needed.

The Input

The input consist on a number in the ' the ' indicating ' number M of programs to make, and followed by a blank line. Then There are M test cases, each one consisting on a single integer n on a line by itself with1 $ leq$ N $ leq$ 8. There is a blank line between test cases.

The Output

The output is M compilable standard Pascal Programs meeting the criteria specified above. Print a blank line between two consecutive programs.

Sample Input

1

3

Sample Output

Program sort (input,output);

Var

A,b,c:integer;

Begin

READLN (A,B,C);

If a < b then

If b < C then

Writeln (A,B,C)

else if a < C then

Writeln (A,C,B)

Else

Writeln (C,A,B)

Else

If a < C then

Writeln (B,A,C)

else if B < C then

Writeln (B,c,a)

Else

Writeln (C,b,a)

End.

Complete code:

 01./*0.291ms*/02.  
#include <cstdio> #include <cstring> 05.  
06.int N, vis[30]; 08.inline void Insert (int cur, int m) 09.        {for (int i = m > cur i--) 11.  
Vis[i] = vis[i-1];  
Vis[cur] = m;  
13.} 14. 15.void handle (int deep) 16.        {for (int i = 0; I <= deep; i++) 18.  
printf ("");    if (deep = n-1) 20.  
{to printf ("Writeln (%c", vis[0] + ' a ');            for (int i = 1; i < n; i++) 23.  
printf (",%c", vis[i] + ' a ');  
Puts (")");  
return;    26.} 27.  
int rec[30];        for (int i = 0; I <= deep i++) 29.  
Rec[i] = vis[i];    for (int i = deep + 1; I >= 0; i--) 31.  
{. if (i) printf ("If%c <%c then\n", vis[i-1] + ' a ', deep + 1 + ' a ');  
Insert (I, deep + 1);  
Handle (deep + 1);  
memcpy (Vis, REC, sizeof (REC));        if (i) 37. { 
for (int i = 0; I <= deep; i++) 39.  
printf ("");  
printf ("Else");        41.} 42.  
if (i = = 1) putchar (' \ n ');  
43.} 44.} 46.int Main (void) 47.  
{A. int t;  
scanf ("%d", &t);    while (t--) 51.  
{memset (Vis, 0, sizeof (VIS));  
scanf ("%d", &n);  
printf ("program sort (input,output); \nvar\na");            for (int i = 1; i < n; i++) 56.  
printf (",%c", ' a ' + i);  
printf (": integer;\nbegin\n readln (a");            for (int i = 1; i < n; i++) 59.  
printf (",%c", ' a ' + i);  
Puts (");");        61.///62.  
Handle (0);        63.///64.  
Puts ("end.");  
Putchar. if (t) (' \ n ');    66.} 67.  
return 0; 
.}

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.