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
- after the program header: program sort(input,output);
- after the variable declaration: ...: integer;
- 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.
Miguel Revilla 2001-05-25 推荐博客:http://www.cnblogs.com/java20130723/p/3212108.html 代码:(没有缩进处理)
#include <cstdio>
const int maxn = ; int n, arr[maxn]; void insert_sort(int p, int c) { //插入排序
for (int i = c; i > p; i--)
arr[i] = arr[i - ];
arr[p] = c;
} int dfs(int d) {
int tmp[d + ]; //创建数组储存原来的数值,不然会乱掉
for (int j = ; j <= n; j++)
tmp[j] = arr[j];
for (int i = d; i >= ; i--) { //循环从现排好的串后序进行dfs
printf("if %c < %c then\n", arr[i] + 'a' - , d + 'a');
insert_sort(i + , d + ); //将接下去的字母插入到i+1的位置
if (d + == n) { //dfs到最深处,输出
printf("writeln(");
printf("%c", arr[] + 'a' - );
for (int j = ; j <= d + ; j++)
printf(",%c", arr[j] + 'a' - );
printf(")\n");
printf("else\n");
}
else {
dfs(d + );
printf("else\n");
}
for (int j = ; j <= n; j++) //还原数组
arr[j] = tmp[j];
}
insert_sort(, d + ); //下面是对最后一个情况,即字母插到整个数组前面,这里是没有else的
if (d + == n) {
printf("writeln(");
printf("%c", arr[] + 'a' - );
for (int j = ; j <= d + ; j++)
printf(",%c", arr[j] + 'a' - );
printf(")\n");
}
else
dfs(d + );
for (int i = ; i <= n; i++)
arr[i] = tmp[i];
} int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
//前面部分
printf("program sort(input,output);\nvar\n");
printf("a");
for (int i = ; i <= n; i++)
printf(",%c", i + 'a' - );
printf(" : integer;\nbegin\nreadln(");
printf("a");
for (int i = ; i <= n; i++)
printf(",%c", i + 'a' - );
printf(");\n");
dfs(); //开始深搜
printf("end.\n");
if (t != )
printf("\n");
}
return ;
}