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.
题意:就是模拟pascal用if,else进行排序。。输入n。输出n个字母的排序过程
思路:大概就是插入排序。一个字母一个字母插进去。每个字母都可以插到当前串的各个位置中去。。。进行回溯输出即可。。但是这输出格式真的挺麻烦的啊啊啊
#include <stdio.h>
#include <string.h> int t, n, i;
int cc[10];
void swap(int &a, int &b)
{
int t = a;
a = b;
b = t;
}
void insert(int num, int *cc) {
int i;
if (num == n)
{
int nu = num * 2;
while (nu --) {printf(" "); }
printf("writeln(");
for (i = 0; i < n - 1; i++) {
printf("%c,", cc[i] + 'a');
}
printf("%c)\n", cc[n - 1] + 'a');
return;
}
int c[10];
for (i = 0; i < num; i ++)
c[i] = cc[i];
for (i = num; i >= 0; i --) {
if (num == i) {
int nu = num * 2;
while (nu --) {printf(" "); }
c[i] = i;
printf("if %c < %c then\n", c[i - 1] + 'a', num + 'a');
insert(num + 1, c);
}
else if (i == 0) {
int nu = num * 2;
while (nu --) {printf(" "); }
printf("else\n");
swap(c[i], c[i + 1]);
insert(num + 1, c);
}
else {
int nu = num * 2;
while (nu --) {printf(" "); }
printf("else if %c < %c then\n", c[i - 1] + 'a', num + 'a');
swap(c[i], c[i + 1]);
insert(num + 1, c);
}
}
}
void out() {
printf("program sort(input,output);\n");
printf("var\n");
for (i = 0; i < n - 1; i++) {
printf("%c,",'a' + i);
}
printf("%c : integer;\n", 'a' + n - 1);
printf("begin\n");
printf(" readln(");
for (i = 0; i < n - 1; i++) {
printf("%c,",'a' + i);
}
printf("%c);\n", 'a' + n - 1);
insert(1, cc);
printf("end.\n");
if (t) printf("\n");
} int main() {
scanf("%d", &t);
while (t --) {
memset(cc, 0, sizeof(cc));
scanf("%d", &n);
out();
}
return 0;
}
UVA 110 Meta-Loopless Sorts(输出挺麻烦的。。。)的更多相关文章
- Uva 110 - Meta-Loopless Sorts(!循环,回溯!)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- uva 110 Meta-Loopless Sorts 用程序写程序 有点复杂的回溯水题
题目要求写一个直接用比较排序的pascal程序,挺有趣的一题. 我看题目数据范围就到8,本来以为贪个小便宜,用switch输出. 然后发现比较次数是阶乘级别的,8的阶乘也是挺大的,恐怕会交不上去. 于 ...
- UVA 11419SAM I AM(输出 最小覆盖点 )
参考博客:如何找取 最小覆盖点集合 题意:R*C大小的网格,网格上面放了一些目标.可以再网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标,计算最小多少子弹,各从哪些位置发射 ...
- Euler Circuit UVA - 10735(混合图输出路径)
就是求混合图是否存在欧拉回路 如果存在则输出一组路径 (我就说嘛 咱的代码怎么可能错.....最后的输出格式竟然w了一天 我都没发现) 解析: 对于无向边定向建边放到网络流图中add(u, v, 1) ...
- Hard Life UVA - 1389(最大密度子图 输出点集)
题意: rt 解析: 我用的第二种方法... s向所有的边连权值为1的边 所有的点向t连权值为mid的边 如果存在u - > v 则边向u和v分别连一条权值为INF的边 二分mid 用dfs ...
- uva 400 Unix ls 文件输出排版 排序题
这题的需要注意的地方就是计算行数与列数,以及输出的控制. 题目要求每一列都要有能够容纳最长文件名的空间,两列之间要留两个空格,每一行不能超过60. 简单计算下即可. 输出时我用循环输出空格来解决对齐的 ...
- SAM I AM UVA - 11419(最小顶点覆盖+输出一组解)
就是棋盘问题输出一组解 https://blog.csdn.net/llx523113241/article/details/47759745 http://www.matrix67.com/blog ...
- decode-string(挺麻烦的)
Java String作为参数传参是不会改变的,这个与常识的感觉不同. public String decodeString(String s) { s = ""; return ...
- UVA题解二
UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...
随机推荐
- SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-005-定义切面使用@Aspect、@EnableAspectJAutoProxy、<aop:aspectj-autoproxy>
一. 假设有如下情况,有一个演凑者和一批观众,要实现在演凑者的演凑方法前织入观众的"坐下"."关手机方法",在演凑结束后,如果成功,则织入观众"鼓掌& ...
- Android ServiceConnection
绑定到一个Service 应用组件(客户端)可以调用bindService()绑定到一个service.Android系统之后调用service的onBind()方法,它返回一个用来与service交 ...
- 使用Sqoop从mysql向hdfs或者hive导入数据时出现的一些错误
1.原表没有设置主键,出现错误提示: ERROR tool.ImportTool: Error during import: No primary key could be found for tab ...
- linux下跨服务器文件文件夹的复制
文件的复制:scp –P (端口号) ./authorized_keys berchina@hadoop002:/home/berchina 文件夹的复制:scp -r -P (端口号) /home/ ...
- Android 国际化字符串资源,各语言目录表及从字符串资源文件读字符方法
字符串国际化 只要在 res 目录下 建对应语言的 values 目录即可. res下values是所有资源(包括字符串资源)的默认目录,如果没有特指资源目录或找不到某资源时,就用它. 如values ...
- Reflector+Reflexil 相结合实现对DLL文件修改
在工作过程中,我们有可能遇到这样的问题:公司发给客户的软件包突然报错了,但是你知道哪里报错了,而这个代码已经编译成DLL文件了,源代码不在自己这里.怎么办呢?还好现在有Reflexil插件,这个插件只 ...
- ☀【Grunt】no such file or directory, imagemin
JPG image optimization not workinghttps://github.com/gruntjs/grunt-contrib-imagemin/issues/16 npm un ...
- C#用xpath查找某节点
C#用xpath查找某节点 从根节点一直下来的相对路径才能确定Xpath的写法. /root/<节点1>/<节点2>//<@属性> Xpath是功能很强大的,但是也 ...
- linux cross toolsChain 交叉编译 ARM(转)
转载请注明出处:http://blog.csdn.net/mybelief321/article/details/9076583 安装环境 Linux版本:Ubuntu 12.04 内核版本:L ...
- mysql 查看死锁和去除死锁
1.查询是否锁表show OPEN TABLES where In_use > 0; 2.查询进程 show processlist 3. 查询到相对应的进程,然后 kill id 验证(ki ...