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 nwriteln 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 ab, 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(输出挺麻烦的。。。)的更多相关文章

  1. Uva 110 - Meta-Loopless Sorts(!循环,回溯!)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. uva 110 Meta-Loopless Sorts 用程序写程序 有点复杂的回溯水题

    题目要求写一个直接用比较排序的pascal程序,挺有趣的一题. 我看题目数据范围就到8,本来以为贪个小便宜,用switch输出. 然后发现比较次数是阶乘级别的,8的阶乘也是挺大的,恐怕会交不上去. 于 ...

  3. UVA 11419SAM I AM(输出 最小覆盖点 )

    参考博客:如何找取 最小覆盖点集合 题意:R*C大小的网格,网格上面放了一些目标.可以再网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标,计算最小多少子弹,各从哪些位置发射 ...

  4. Euler Circuit UVA - 10735(混合图输出路径)

    就是求混合图是否存在欧拉回路 如果存在则输出一组路径 (我就说嘛 咱的代码怎么可能错.....最后的输出格式竟然w了一天 我都没发现) 解析: 对于无向边定向建边放到网络流图中add(u, v, 1) ...

  5. Hard Life UVA - 1389(最大密度子图 输出点集)

    题意: rt 解析: 我用的第二种方法... s向所有的边连权值为1的边 所有的点向t连权值为mid的边 如果存在u -  > v  则边向u和v分别连一条权值为INF的边 二分mid 用dfs ...

  6. uva 400 Unix ls 文件输出排版 排序题

    这题的需要注意的地方就是计算行数与列数,以及输出的控制. 题目要求每一列都要有能够容纳最长文件名的空间,两列之间要留两个空格,每一行不能超过60. 简单计算下即可. 输出时我用循环输出空格来解决对齐的 ...

  7. SAM I AM UVA - 11419(最小顶点覆盖+输出一组解)

    就是棋盘问题输出一组解 https://blog.csdn.net/llx523113241/article/details/47759745 http://www.matrix67.com/blog ...

  8. decode-string(挺麻烦的)

    Java String作为参数传参是不会改变的,这个与常识的感觉不同. public String decodeString(String s) { s = ""; return ...

  9. UVA题解二

    UVA题解二 UVA 110 题目描述:输出一个Pascal程序,该程序能读入不多于\(8\)个数,并输出从小到大排好序后的数.注意:该程序只能用读入语句,输出语句,if语句. solution 模仿 ...

随机推荐

  1. 开启CURL扩展,让服务器支持PHP curl函数(远程采集)

    关于开启Curl的方法模板天下小编在此给大家简单说一下 curl().file_get_contents().snoopy.class.php这三个远程页面抓取或采集中用到的工具,默迹还是侵向于用sn ...

  2. ANDROID_MARS学习笔记_S01原始版_013_广播机制二

    一.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  3. Fast UI Draw (Intel出品)

    Fast UI Draw in a library that provides a higher performance Canvas interface. It is designed so tha ...

  4. 【HDOJ】1720 A+B coming

    水题. #include <stdio.h> #include <string.h> #define MAXNUM 1005 int stoi(char); int main( ...

  5. Codeforces Round #195 (Div. 2)

    记次CF吧 1题...B题..因为循环的i没设成long long 却参与了运算 结果就悲剧了 一直交 一直挂 ..上题 A 水.. 第一次少了个空格还.. #include <iostream ...

  6. 虚拟主机 (Virtual Host)

    虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...

  7. 【转】NI语法 JNI参考 JNI函数大全

    原文网址:http://blog.sina.com.cn/s/blog_5de73d0b0101chk1.html 一.对照表 Java类型    本地类型         描述boolean     ...

  8. jQuery里面的datepicker日期控件默认是显示英文的,如何显示中文或其他语言呢?

    jQuery里面的datepicker日期控件默认是显示英文的,如何让他显示中文或其他呢? [官方的写法]: (1)引入JS文件: <script type="text/javascr ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.1

    For fixed basis of in $\scrH$ and $\scrK$, the matrix $A^*$ is the conjugate transpose of the matrix ...

  10. linux tar 解压命令总结

    把常用的tar解压命令总结下,当作备忘: tar -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其 ...