Uva oj 110-meta-loopless sorts (non-cyclic-meta sorting)

Source: Internet
Author: User

Time Limit: 3.000 seconds
Time Limit: 3.000 seconds

Background
Background

Sorting holds an important place in computer science. analyzing and implementing varous sorting algorithms forms an important part of the education of most computer scientists, and sorting accounts for a significant percentage of the world's computational resources. sorting algorithms range from the bewilderingly popular bubble sort, to quicksort, to parallel sorting algorithms and sorting networks. in this problem you will be writing a program that creates a sorting program (a meta-sorter ).
SortAlgorithmThe position in computer science is very important. Analysis and Implementation of various sorting algorithms are also an important part in the teaching practice of most computer systems. Sorting accounts for a large proportion of computing resources around the world. From the confusing famous Bubble sorting and quick sorting to the parallel sorting and sorting grid, various sorting algorithms are everywhere. In this case, you need to writeProgramGenerate a sorting algorithm (meta-sorting ).

The Problem
Problem

The problem is to create several programs whose output is a standard PASCAL program that sorts n numbers where N is the only input to the program you will write. the Pascal programs generated by your program must have the following properties:
This problem is that the program to be written is used to output the sorting of another standard Pascal.CodeTo sort the specified n numbers in the input. The Pascal code generated by your program must meet the following requirements:

  • They must begin with program sort (input, output );
    It must start with "program sort (input, output );"
  • They must declare storage for exactly n integer variables. The names of the variables must come from the first n letters of the alphabet (a, B, c, d, e, f ).
    The bucket must be declared accurately for N integer variables. The variable names must be arranged by the first n letters in the alphabet (a, B, c, d, e, f ).
  • A single readln statement must read in values for all the integer variables.
    You must use the unique "readln" statement to read all 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 variables. exactly n! Writeln statements must appear in the program.
    In addition to the "writeln" Statement, only the "if the else" statement can appear. The judgment condition of each "if" statement must be strictly not equal to the previous operand (<or> ). Only n must appear in the program! Writeln statements.
  • Exactly three semi-colons must appear in the programs
    The entire procedure has only three disciplinary numbers.
    1. After the program header: program sort (input, output );
      After the program header: program sort (input, output );
    2. After the variable Declaration:...: integer;
      After the variable Declaration:...: integer;
    3. After the readln statement: readln (...);
      Behind the "readln" Statement: readln (...);
  • No redundant comparisons of Integer Variables shocould be made. For example, during program execution, once it is determined that a <B, variables A and B shocould not be compared again.
    Do not compare integer variables. For example, when a <B is determined by comparison during code execution, the two variables A and B cannot be compared again.
  • Every writeln statement must appear on a line by itself.
    Each "writeln" Statement occupies only one row.
  • The programs must compile. Executing the program with input consisting of any arrangement of any n distinct integer values shocould result in the input values being printed in sorted order.
    The output code must be compiled. The code must be able to run the correct sorting operation: Enter N values in any order, and then output the N values.

For those unfamiliar with Pascal syntax, the example at the end of this problem completely defines the small subset of Pascal needed.
For those who are not familiar with Pascal syntax, the following example defines a complete subset of Pascal syntax required to understand the question.

The input
Input

The input consist on a number in the first line indicating the number M of programs to make, followed by a blank line. then there are m test cases, each one consisting on a single integer n on a line by itself with 1 ≤ n ≤ 8. there will be a blank line between test cases.
The first line is a number M, which indicates the code segments to be generated. Below is a blank line. Then there are m test cases. Each test case is an integer n that excludes a single row and must be 1 ≤ n ≤ 8. There is an empty line between the two test cases.

The output
Output

The output is m compilable standard Pascal programs meeting the criteria specified abve. Print a blank line between two consecutive programs.
Output the standard Pascal code that can be compiled in the M segment according to the above definition. Print an empty line between every two sections of code.

Sample Input
Sample Input

1

 

3

 

Sample output
Sample output
 
Program sort (input, output); VARA, B, c: integer; begin readln (A, B, C); if a <B then if B <C then writeln (, b, c) else if a <C then writeln (A, C, B) else writeln (C, A, B) else if a <C then writeln (B,, c) else if B <C then writeln (B, C, A) else writeln (C, B, A) end.

 

Analysis
Analysis

The key lies in how to generate a full arrangement of a given number of elements, which naturally uses recursive algorithms. Consider n = 3. The three variables are A, B, and C. After a is added to the result set as initialization, B can be inserted before or after a, representing B <A or B> A respectively. In this way, you can generate an IF-else Statement of level 2 and call it recursively to level 2. For the result set in the "ba" sequence, C can be inserted in three positions. The corresponding statement should be:

 
If C <B then... else if C <A then... else...

A 2nd-level if-else statement is generated. Recursively call the code to level 3rd. If the code reaches the highest level, output the result and return it.

This question has a special note. do not have any blank lines before the first code is generated. It does not matter after each code segment. Instead, it starts with the second code and adds a blank line to the header. If you do not pay attention to this problem, you will get pe.

 

Solution
# Include <iostream> # include <string> using namespace STD; const char * pvars = "abcdefgh"; const char * indent (INT ncnt) {static const char * pspaces [] = {"","","","","","","","","", ""}; return pspaces [ncnt];} void metasort (INT ncurlv, int nmaxlv, string & strorder) {If (ncurlv = nmaxlv) {cout <indent (ncurlv) <"writeln (" <strorder [0]; for (size_t I = 1; I <strorder. size (); ++ I) {cou T <',' <strorder [I];} cout <") \ n"; return;} cout <indent (ncurlv ); for (size_t I = 0; I <strorder. size (); ++ I) {cout <"if" <pvars [ncurlv] <"<" <strorder [I] <"then \ n "; strorder. insert (I, pvars + ncurlv, 1); metasort (ncurlv + 1, nmaxlv, strorder); strorder. erase (I, 1); cout <indent (ncurlv) <"else" ;}cout <Endl; strorder. insert (strorder. size (), pvars + ncurlv, 1); metasort (ncurlv + 1, nmaxlv, strorder); strorder. erase (strorder. size ()-1, 1) ;}int main (void) {int nprogs, nvars; CIN >>> nprogs; For (Int J = 0; j <nprogs; ++ J) {CIN> nvars; If (J! = 0) {cout <Endl;} string strvardec = "A"; for (INT I = 1; I <nvars; ++ I) {strvardec. push_back (','); strvardec. push_back (I + 'A');} cout <"program sort (input, output); \ nvar \ n" <strvardec <": integer; \ nbegin \ n readln ("<strvardec <"); \ n "; string strinit (" A "); metasort (1, nvars, strinit ); cout <"end. "<Endl;} 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.