程序控

IPPP (Institute of Penniless Peasent-Programmer) Fellow

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::
  77 随笔 :: 0 文章 :: 442 评论 :: 0 引用

Time limit: 3.000 seconds
限时:3.000秒


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:
这个问题是要写的程序是用来输出另一个标准Pascal的排序代码,可以对输入中指定的n个数进行排序。你的程序生成的Pascal代码必须符合以下要求:

  • They must begin with program sort(input,output);
    必须由“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).
    必须准确的为n个整形变量声明存储空间。变量名必须按照字母表中的前n个字母排列(a,b,c,d,e,f)。
  • A single readln statement must read in values for all the integer variables.
    必须使用唯一的“readln”语句读取全部整型变量
  • 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.
    后面除了“writeln”语句外,只能出现“if the else”语句。每个“if”语句的判断条件必须由严格的不等于以前后操作数构成(< 或 >)。程序中必须出现且仅出现n!个writeln语句。
  • Exactly three semi-colons must appear in the programs
    整个程序中只有三处分号
    1. after the program header: program sort(input,output);
      在程序头的后面:program sort(input,output);
    2. after the variable declaration: ...: integer;
      在变量声明的后面:...: integer;
    3. after the readln statement: readln(...);
      在“readln”语句的后面: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.
    不要对整型变量做多余的比较。比如在代码运行过程中,一但通过比较确定了a < b,那么a和b这两个变量就不能再次进行比较。
  • Every writeln statement must appear on a line by itself.
    每条“writeln”语句都独占一行
  • 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.
    输出的代码必须可以编译。执行代码必须能够执行正确的排序运算:输入任意排列的n个值,排序后再输出这n个值。

For those unfamiliar with Pascal syntax, the example at the end of this problem completely defines the small subset of Pascal needed.
对于不熟悉Pascal语法的同学,下面的例子中完整的定义了解题所需的Pascal语法子集。


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.
输入的第一行是一个数字M,表示你要生成几段代码,下面是一个空行。然后是M个测试用例,每个测试用例均为独占一行的整数n,并满足1 ≤ n ≤ 8。两个测试用例的输入间有一个空行。


The Output
输出

The output is M compilable standard Pascal programs meeting the criteria specified above. Print a blank line between two consecutive programs.
按照上述定义输出M段可编译的标准Pascal代码。在每两段代码之间打印一个空行。


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.

 

Analysis
分析

关键在于如何生成给定数量元素的全排列,很自然的使用递归算法。考虑n = 3的情况,相应的3个变量为a, b, c。在结果集里加入a作为初始化,之后b可以在a前面或后面2种位置可以插入,分别代表b < a或b > a的情况。这样就可以生成第1级的if-else语句,并递归调用至2级。对于"ba"顺序的结果集,c有三种位置可以插入,对应的语句应该是:

if c < b then
...
else if c < a then
...
else
...

由此生成第2级的if-else语句。再递归调用至第3级,发现已到达最高级,输出结果并返回。

这道题有一个需要特别注意的地方,生成的第一道代码前不要有任何空行,每段代码后也不要紧跟着空行。而是从第二段代码开始,头部需要加一个空行。如果没有注意这个问题,会得到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) {
			cout << ',' << 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;
}
posted on 2010-08-10 03:05  Devymex  阅读(...)  评论(...编辑  收藏