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.

 

思路:模拟插入排序,一个一个元素插入,插入元素时从后往前比较。

#include<cstdio>
#include<string>
using namespace std;
int n;
string str;
static const char *indent[] = { "",
" ", " ", " ", " ", " ", " ",
" ", " ", " " };
char alphas[]="abcdefgh"; void dfs(int cur)
{
if(cur==n)
{
printf("%s", indent[cur]);
printf("writeln(%c", str[0]);
for (int i = 1; i < n; i++)
printf(",%c", str[i]);
printf(")\n");
return;
}
//从尾部向前插
for(int i=str.size()-1; i>=0; i--)
{
printf("%s", indent[cur]);
if(i!=(int)str.size()-1)
printf("else ");
printf("if %c < %c then\n", str[i], alphas[cur]);
str.insert(i+1, 1, alphas[cur]);
dfs(cur+1);
str.erase(i+1, 1);
}
//插入到最前面
printf("%s", indent[cur]);
printf("else\n");
str.insert(0, 1, alphas[cur]);
dfs(cur+1);
str.erase(0, 1);
} int main()
{
int T;
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
printf("program sort(input,output);\nvar\na");
for(int i=1;i<n;i++)
printf(",%c", 'a'+i);
printf(" : integer;\n");
printf("begin\n");
printf(" readln(a");
for (int i = 1; i < n; i++)
printf(",%c", 'a' + i);
puts(");");
str="a";
dfs(1);
puts("end."); if(T)
printf("\n");
}
return 0;
}

Uva110 Meta-Loopless Sorts的更多相关文章

  1. 常用 meta 整理

    <!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 --> <meta name="HandheldFriendly" con ...

  2. meta标签

    参考:http://www.jb51.net/web/158860.html META标签分两大部分:HTTP标题信息(HTTP-EQUIV)和页面描述信息(NAME). 一.HTTP标题信息(HTT ...

  3. Django模型类Meta元数据详解

    转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...

  4. H5 meta小结

    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1, ...

  5. Asp.net 后台添加CSS、JS、Meta标签

    Asp.net 后台添加CSS.JS.Meta标签的写法,我这里写成函数方便以后使用.如果函数放在页面类中, Page参数也可以不要. 首先导入命名空间 using System.Web.UI.Htm ...

  6. 较为完整的meta

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 浏览器内核控制Meta标签说明文档【转】

    背景介绍 由于众所周知的情况,国内的主流浏览器都是双核浏览器:基于Webkit内核用于常用网站的高速浏览.基于IE的内核用于兼容网银.旧版网站.以360的几款浏览器为例,我们优先通过Webkit内核渲 ...

  8. HTML <meta> 标签,搜索引擎

    关于Mate标签的详尽解释,请查看w3school 网址为:http://www.w3school.com.cn/tags/tag_meta.asp meta标签作用 META标签是HTML标记HEA ...

  9. 内核控制Meta标签:让360浏览器默认使用极速模式打开网页(转)

    为了让网站页面不那么臃肿,也懒的理IE了,同时兼顾更多的国内双核浏览器,在网页页头中添加了下面两行Meta控制标签. 1,网页头部加入 <meta name="renderer&quo ...

随机推荐

  1. redis介绍【转】

    Redis新的存储模式diskstore Thursday, Jan 6th, 2011 by Tim | 13 CommentsFiled under: data | Tags: Mongo, Mo ...

  2. Multi-Device Hybrid Apps for Visual Studio CTP2.0

    http://msdn.microsoft.com/en-us/library/dn771545.aspx http://www.microsoft.com/en-us/download/detail ...

  3. Locker

    题意: 有2个数字串,每次可以变化1-3位(每位+1或-1(0-9,9-0)可循环),求由1串变到2串的最小用的次数. 分析: dp[i][num]表示变到第i位时最后两位组成的数是num时最小次数( ...

  4. 【LeetCode 236】Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. jQuery 关于点击菜单项,使子条目“向上”展开效果的实现

    为什么做了这样一个的功能呢?原因是前一段时间jQuery群里有个朋友想实现这样一个东东,大家都知道jQuery中有现成的slideDown和slideUp方法,但那是向下展开,而这个是一个完全相反的效 ...

  6. 学习Python必须要知道的常用模块

    在程序设计中,为完成某一功能所需的一段程序或子程序:或指能由编译程序.装配程序等处理的独立程序单位:或指大型软件系统的一部分.本文为你介绍了Python中的两种常用模块. os: 这个模块包含普遍的操 ...

  7. Epic - Snake Sequence

    You are given a grid of numbers. A snakes equence is made up of adjacent numbers such that for each ...

  8. Hadoop 2 初探

    Hadoop 2.6.0的安装略复杂,在一台既有Hadoop 1又有Hadoop 2的server上,要设置好环境变量,必要时候echo $HADOOP_HOME一下看运行的是哪个版本. Master ...

  9. AC多模式匹配算法

    建议:学习ac算法最好的途径是看论文pdf_Efficient_String_Matching_An_Aid_to_Biblio 一.一般的搜索算法 keyword: { he, she, his, ...

  10. 分区表,桶表,外部表,以及hive一些命令行小工具

    hive中的表与hdfs中的文件通过metastore关联起来的.Hive的数据模型:内部表,分区表,外部表,桶表受控表(managed table):包括内部表,分区表,桶表 内部表: 我们删除表的 ...