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. 关于用netbeans和xdebug调试php的配置

    之前用过一段时间在apache,netbeans下通过xdebug调试.感觉不错,最近事情不多想从新配置下,是基于最新版本的php5.4做的,后来参考了下xdebug的官网说明完成的.官网地址:htt ...

  2. 【转】WPF MultiBinding 和 IMultiValueConverter

    WPF MultiBinding 和 IMultiValueConverter 时间 2015-02-02 19:43:00  博客园精华区 原文  http://www.cnblogs.com/wo ...

  3. 使用RoboCopy 命令

    经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...

  4. 几种ESB(企业服务总线)介绍

    ESB(Enterprise Service Bus,即企业服务总线)是传统中间件技术与XML.Web服务等技术结合的产物.ESB提供了网络中最基本的连接中枢,是构筑企业神经系统的必要元素. 企业服务 ...

  5. table应用之colspan与rowspan

    <table border=" borderColorDark="#66ff33"> <tr> <td rowspan=" ali ...

  6. 【LeetCode】101 - Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  7. Visual Studio 2010+Oracle 10g +NHibernate配置

    南京酷都面试,考官问:你知道NHibernate吗?瞬间我就急了:只听说过Hibernate,NHibernate是什么?还有其他问题也是不知道,所以后果就悲剧了. 自己做一个小系统,总是想如果数据量 ...

  8. Linux-sed用法

    本文为转载,原地址:http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html sed是一个很好的文件处理工具,本身是一个管道命 ...

  9. js运动 淡入淡出

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. java、android 对比两个目录或文件是否是同一个目录或文件的方法

    由于软链接及android的外部卡mount方式存在,导致一个文件夹可能同时有两个路径,如: /mnt/sdcard1      /storage/ext_sdcard ,如果通过某种方式(如moun ...