Uva 110 - Meta-Loopless Sorts(!循环,回溯!)
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.
Miguel Revilla 2001-05-25 推荐博客:http://www.cnblogs.com/java20130723/p/3212108.html 代码:(没有缩进处理)
#include <cstdio>
const int maxn = ; int n, arr[maxn]; void insert_sort(int p, int c) { //插入排序
for (int i = c; i > p; i--)
arr[i] = arr[i - ];
arr[p] = c;
} int dfs(int d) {
int tmp[d + ]; //创建数组储存原来的数值,不然会乱掉
for (int j = ; j <= n; j++)
tmp[j] = arr[j];
for (int i = d; i >= ; i--) { //循环从现排好的串后序进行dfs
printf("if %c < %c then\n", arr[i] + 'a' - , d + 'a');
insert_sort(i + , d + ); //将接下去的字母插入到i+1的位置
if (d + == n) { //dfs到最深处,输出
printf("writeln(");
printf("%c", arr[] + 'a' - );
for (int j = ; j <= d + ; j++)
printf(",%c", arr[j] + 'a' - );
printf(")\n");
printf("else\n");
}
else {
dfs(d + );
printf("else\n");
}
for (int j = ; j <= n; j++) //还原数组
arr[j] = tmp[j];
}
insert_sort(, d + ); //下面是对最后一个情况,即字母插到整个数组前面,这里是没有else的
if (d + == n) {
printf("writeln(");
printf("%c", arr[] + 'a' - );
for (int j = ; j <= d + ; j++)
printf(",%c", arr[j] + 'a' - );
printf(")\n");
}
else
dfs(d + );
for (int i = ; i <= n; i++)
arr[i] = tmp[i];
} int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
//前面部分
printf("program sort(input,output);\nvar\n");
printf("a");
for (int i = ; i <= n; i++)
printf(",%c", i + 'a' - );
printf(" : integer;\nbegin\nreadln(");
printf("a");
for (int i = ; i <= n; i++)
printf(",%c", i + 'a' - );
printf(");\n");
dfs(); //开始深搜
printf("end.\n");
if (t != )
printf("\n");
}
return ;
}
Uva 110 - Meta-Loopless Sorts(!循环,回溯!)的更多相关文章
- uva 110 Meta-Loopless Sorts 用程序写程序 有点复杂的回溯水题
题目要求写一个直接用比较排序的pascal程序,挺有趣的一题. 我看题目数据范围就到8,本来以为贪个小便宜,用switch输出. 然后发现比较次数是阶乘级别的,8的阶乘也是挺大的,恐怕会交不上去. 于 ...
- UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)
Meta-Loopless Sorts Background Sorting holds an important place in computer science. Analyzing and ...
- uva 387 A Puzzling Problem (回溯)
A Puzzling Problem The goal of this problem is to write a program which will take from 1 to 5 puzz ...
- UVa 524 Prime Ring Problem【回溯】
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...
- UVA 140 Brandwidth 带宽 (dfs回溯)
看到next_permutation好像也能过╮(╯▽╰)╭ 这题学习点: 1.建图做映射 2.通过定序枚举保证字典序最小 3.strtok,sscanf,strchr等函数又复习了一遍,尽管程序中没 ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- UVA 524 素数环 【dfs/回溯法】
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...
- UVA - 12113 Overlapping Squares(dfs+回溯)
题目: 给定一个4*4的棋盘和棋盘上所呈现出来的纸张边缘,问用不超过6张2*2的纸能否摆出这样的形状. 思路: dfs纸的张数,每一张中枚举这张纸左上角这个点的位置,暴力解题就可以了. 这个题的覆盖太 ...
- UVA - 225 Golygons (黄金图形)(回溯)
题意:平面有k个障碍点.从(0,0)出发,第一次走1个单位,……,第n次走n个单位,恰好回到(0,0),每次必须转弯90°,图形可以自交,但不能经过障碍点.按字典序输出所有移动序列,并输出序列总数. ...
随机推荐
- [Design Patterns] 1. Primary concept & term - UML
It's time to review design patterns, especially when I reach the turning-point of my career. That's ...
- [转载]Windows 2012 R2安装SharePoint 2013 手动安装工具软件
之前介绍过在window 2012中安装SharePoint 2013,这次,借着SharePoint 2013 sp1补丁发布之际,介绍下在window 2012 r2中安装SharePoint 2 ...
- Python单元测试框架之pytest---如何执行测试用例
介绍 pytest是一个成熟的全功能的Python测试工具,可以帮助你写出更好的程序. 适合从简单的单元到复杂的功能测试 l 模块化parametrizeable装置(在2.3,持续改进) l 参 ...
- PintJS – 轻量,并发的 GruntJS 运行器
PintJS 是一个小型.异步的 GruntJS 运行器,试图解决大规模构建流程中的一些问题. 典型的Gruntfile 会包括 jsHint,jasmine,LESS,handlebars, ugl ...
- Android 开发有用代码积累
Android开发需求变化快,开发周期要求尽量短,接下来一系列文章从实际使用出发总结一些常用的代码片段,便于查找,也为后来人提供一份参考. 1.获取Manifest的基本信息(升级页面和软件关于页面一 ...
- 基于openssl的单向和双向认证
1.前言 最近工作涉及到https,需要修改nginx的openssl模块,引入keyless方案.关于keyless可以参考CloudFlare的官方博客: https://blog.cloudfl ...
- IOS开发UI基础UIImageView属性属性
UIImageView属性 1.Image 设置图片,默认显示 UIImageView *_imageView = [[UIImageView alloc]init]; _imageView.imag ...
- IOS开发UI基础storyboard相关概念的认识
本文主要介绍一些基本的概念 为后面的学习做个准备 需要了解的知识点有以下几个方面: storyboard文件的认识 IBAction 和IBOutlet UIViewController控制器的认识 ...
- 连续值的CART(分类回归树)原理和实现
上一篇我们学习和实现了CART(分类回归树),不过主要是针对离散值的分类实现,下面我们来看下连续值的cart分类树如何实现 思考连续值和离散值的不同之处: 二分子树的时候不同:离散值需要求出最优的两个 ...
- Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划)
Scrum 3.1 多鱼点餐系统开发进度(第三阶段项目构思与任务规划) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到 ...