UVA - 110 Meta-Loopless Sorts

来源:互联网 发布:vivo授权网络怎么弄 编辑:程序博客网 时间:2024/04/30 15:05

  Meta-Loopless Sorts 

Background 

Sorting holds an important place in computer science. Analyzing and implementing various 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).

The 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:

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

  • They must declare storage for exactly ninteger variables. The names of the variables must come from the first n 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 variables. Exactly n!writeln statements must appear in the program.

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

    2. after the variable declaration: ...: integer;

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

  • No redundant comparisons of integer variables should be made. For example, during program execution, once it is determined thata < 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 of any arrangement of any n distinct integer values should result in the input values being printed in sorted order.

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

The 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 $ \leq$n$ \leq$ 8. There will be 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 

13

Sample Output 

program sort(input,output);vara,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.
题意:
生成所有可能的顺序

递归 ,每次新增一个数字,考虑每个插入点。

#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#define N 100using namespace std;int n, m;int i;int a[N];void insert (int x,int y) {for (int i = y; i > x; i--)a[i] = a[i-1];     a[x] = y; }void fun (int x) {for (int i = 0; i <= x; i++)cout <<"  ";if (x == m - 1) {printf("writeln(%c",a[0] +'a');for (int i = 1; i < m; i++)printf(",%c",a[i] + 'a');cout <<")" <<endl;return ;}int b[N];for (int i = 0; i <= x; i++) {b[i] = a[i];}for (int i = x+1; i >= 0; i--) {if(i) printf("if %c < %c then\n",a[i-1]+'a',x+1+'a');insert(i,x+1);fun(x+1);memcpy(a,b,sizeof(b));if (i) {for (int i = 0; i <= x; i++)cout <<"  ";cout <<"else ";}if (i == 1)cout << endl;} }int main () {cin >> n;while (n--) {cin >> m;memset(a,0,sizeof(a));cout << "program sort(intput,output);" <<endl<<"var"<<endl;for ( i = 0; i < m - 1; i++) //cout << i + 'a'<< ",";printf("%c,",i +'a');if (i == m -1)//cout << i + 'a'<<" : integer;" <<endl<<"begin"<<endl;printf("%c : integer;\nbegin\n",i + 'a');cout << "  readln(a";for (int i = 1; i < m; i++)//cout<<"," << i + 'a';printf(",%c",i + 'a');cout << ");"<<endl;fun (0);cout << "end." <<endl;if (n)cout <<endl;}return 0;}


0 0
原创粉丝点击