Meta-Loopless Sorts
Meta-Loopless Sorts |
Background
Sorting holds an important place in computer science. Analyzing andimplementing various sorting algorithms forms an important part of theeducation of most computer scientists, and sorting accounts for a significantpercentage of the world's computational resources. Sortingalgorithms range from the bewilderingly popular Bubble sort, toQuicksort, to parallel sorting algorithms and sorting networks. In thisproblem 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 standardPascal program thatsorts n numbers where n is the only input to the program you will write.The Pascal programs generated by your program must have the followingproperties:- They must begin with program sort(input,output);
- They must declare storage forexactly n integer variables. The names of thevariables must come from the first n letters of the alphabet (a,b,c,d,e,f).
- A single readlnstatement must read in values for all the integer variables.
- Other than writeln statements, the only statements in theprogram are if then else statements. The boolean conditional foreach if statement must consist of one strict inequality (either <or >) of two integer variables. Exactly n! writeln statementsmust appear in the program.
- Exactly three semi-colons must appear in the programs
- after theprogram 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 bshould not be compared again.
- Every writeln statement must appear on a line by itself.
- The programs must compile. Executing the program with inputconsisting of anyarrangement of any n distinct integer values should result in the inputvalues being printed in sorted order.
For those unfamiliar with Pascal syntax, the example at the end of thisproblem 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![$ \leq$](http://uva.onlinejudge.org/external/1/110img1.png)
![$ \leq$](http://uva.onlinejudge.org/external/1/110img1.png)
The Output
The output is M compilable standardPascal 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个数的无循环的排序算法代码。
简单来说就是将当前的数字插入到当前排列的各个位置。代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iomanip>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif // ACM
work();
}
/***************************************************/
int n;
vector<char> vec;
void dfs(int p)
{
if(p == n)
{
ff(i, 4*p) printf(" ");
printf("writeln(");
printf("%c", vec[0]);
fff(i, 1, n-1) printf(",%c", vec[i]);
puts(")");
return;
}
vec.push_back('a'+p);
bool first = true;
dff(i, p, 1)
{
if(first)
{
ff(i, 4*p) printf(" ");
first = false;
}
else
{
ff(i, 4*p) printf(" ");
printf("else ");
}
printf("if %c < %c then\n", vec[i-1], vec[i]);
dfs(p+1);
swap(vec[i], vec[i-1]);
}
ff(i, 4*p) printf(" ");
puts("else");
dfs(p+1);
vec.erase(vec.begin());
}
void work()
{
int T;
scanf("%d", &T);
fff(cas, 1, T)
{
scanf("%d", &n);
puts("program sort(input,output);");
puts("var");
printf("a");
fff(i, 1, n-1) printf(",%c", 'a'+i);
puts(" : integer;");
puts("begin");
printf(" readln(a");
fff(i, 1, n-1) printf(",%c", 'a'+i);
puts(");");
vec.push_back('a');
dfs(1);
vec.erase(vec.begin());
puts("end.");
if(cas != T) puts("");
}
}