Uva110 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.
思路:模拟插入排序,一个一个元素插入,插入元素时从后往前比较。
#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的更多相关文章
- 常用 meta 整理
<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 --> <meta name="HandheldFriendly" con ...
- meta标签
参考:http://www.jb51.net/web/158860.html META标签分两大部分:HTTP标题信息(HTTP-EQUIV)和页面描述信息(NAME). 一.HTTP标题信息(HTT ...
- Django模型类Meta元数据详解
转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...
- H5 meta小结
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1, ...
- Asp.net 后台添加CSS、JS、Meta标签
Asp.net 后台添加CSS.JS.Meta标签的写法,我这里写成函数方便以后使用.如果函数放在页面类中, Page参数也可以不要. 首先导入命名空间 using System.Web.UI.Htm ...
- 较为完整的meta
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 浏览器内核控制Meta标签说明文档【转】
背景介绍 由于众所周知的情况,国内的主流浏览器都是双核浏览器:基于Webkit内核用于常用网站的高速浏览.基于IE的内核用于兼容网银.旧版网站.以360的几款浏览器为例,我们优先通过Webkit内核渲 ...
- HTML <meta> 标签,搜索引擎
关于Mate标签的详尽解释,请查看w3school 网址为:http://www.w3school.com.cn/tags/tag_meta.asp meta标签作用 META标签是HTML标记HEA ...
- 内核控制Meta标签:让360浏览器默认使用极速模式打开网页(转)
为了让网站页面不那么臃肿,也懒的理IE了,同时兼顾更多的国内双核浏览器,在网页页头中添加了下面两行Meta控制标签. 1,网页头部加入 <meta name="renderer&quo ...
随机推荐
- js match regex
需要返回成数组元素的要放在括号里头 var arr = /input-([0-9]*)-([0-9]*)/.exec(id); var all = arr[0]; var row = arr[1]; ...
- s:iterator标签的使用
1.在说明s:iterator标签的使用前,先了解下struts2中的Value Stack. 这里参考了webwork中对Value Stack的描述,由于struts2是在webwork的基础上进 ...
- 服务器中判断客户端socket断开连接的方法
1, 如果服务端的Socket比客户端的Socket先关闭,会导致客户端出现TIME_WAIT状态,占用系统资源. 所以,必须等客户端先关闭Socket后,服务器端再关闭Socket才能避免TIME_ ...
- pcap文件的文件头的link type
http://www.tcpdump.org/linktypes.html Link-layer header type values LINKTYPE_ name LINKTYPE_ value C ...
- sqlplus常用命令
原文 sqlplus常用命令 desc 表名 显示表的结构 show user 显示当前连接用户 show error 显示错误 sho ...
- java读取目录下所有csv文件数据,存入三维数组并返回
package dwzx.com.get; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; ...
- C#/.net给textbox添加回车事件
前端js代码,放到<head>标签下 <script type="text/javascript"> function EnterTextBox(butto ...
- pthread_cond_timedwait时间设置
最近工作中需要在ACodec中起一个pthread,并每间隔100ms统计一次buffer的状态,在程序中使用pthread_cond_timedwait来设置时间间隔,但在使用中发现当超时时间设置成 ...
- 总结调用Flash的几种方法
一.Adobe 提供的方法 <object width="200" height="200" classid="clsid:D27CDB6E-A ...
- 一、 使用存储过程实现数据分页(Sql Server 2008 R2)
1.废话不多说了,直接上代码.调用这个存储过程只需要传递 表名,排序字段,搜索字段,以及页码,页码数量,搜索值(可空) create PROCEDURE NewPage --通用的分页存储过程,百万数 ...