uva 110 - Meta-Loopless Sorts


  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 n integer 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 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 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 1 n 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 

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.
看了很久才理解题意,按题意排序,每次向一个元素个数为n的序列插入一个新元素,通过n次比较输出所有可能的大小情况
n=1  a   ab ,ba
n=2  在n=1的基础上  abc  acb cab bac bca cba
....
递归的生成所有可能的顺序,每一个if必然有个与之配对的else,输出的时候需要控制当前排列之前是否需要输出一个if,如果要输出if要判断是和上一个else同一行还是换一行。
#include<stdio.h>
#include<string.h>
int n;
void write(int a[])
{int i;
 for (i=1;i<=2*n;i++) printf(" ");
 printf("writeln(%c",'a'+a[1]-1);
 for (i=2;i<=n;i++)
 printf(",%c",'a'+a[i]-1);
 printf(")\n");
}
void insert(int a[],int pos,int num) //插入第num个元素num到当前序列第pos个位置
{int i,x=a[pos];
 for (i=num+1;i>pos;i--)
 a[i]=a[i-1];
 a[pos]=num;
};
void work(int step,int a[])
{int i,j,b[10];
 if (step>=n) {write(a);return;}
 for (j=step+1;j>=1;j--)
 {
  for (i=1;i<=step;i++)
  b[i]=a[i];
  for (i=1;i<=step*2;i++)
  printf(" ");
  if (j==step+1) printf("if %c < %c then\n",'a'+b[j-1]-1,'a'+step); //j=step+1插入第step+1个元素的第一次比较
           else  printf("else");//插入第step+1个元素,不是第一次插入的话,之前一定有一个if已经输出了,所以输出else与之配对
  if ((j>1)&&(j<=step))   printf(" if %c < %c then\n",'a'+b[j-1]-1,'a'+step); //最后一个else后面没有if
  if (j==1) printf("\n");
  insert(b,j,step+1);
  work(step+1,b);
 }
}
int main()
{int t,i,a[10];
 scanf("%d",&t);
 while (t--)
 {
  scanf("%d",&n);
    printf("program sort(input,output);\nvar\na");
  for (i=1;i<n;i++)
    printf(",%c",'a'+i);
    printf(" : integer;\nbegin\n  readln(a");
  for (i=1;i<n;i++)
    printf(",%c",'a'+i);
    printf(");\n");
  a[1]=1;
    work(1,a);
    printf("end.\n");
  if (t) printf("\n");
 }
 return 0;
}

你可能感兴趣的:(Integer,input,pascal,output,sorting,variables)