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. UIcollectionView的使用(首页的搭建4)

    2.5 头部视图

  2. ANDROID_MARS学习笔记_S02_002_Date\TimePicker

    一.文档用法 1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  3. 中国版dropbox“坚果云”和它背后的团队故事(大的优势就在于他为用户提供了设定多个文件夹的权利)

    (速途网专栏 作者:娄昊川)坚果云是一款中文存储服务,前身是“坚果铺子”,提供免费的云空间,与dropbox类似,用户可以直接把档案同步到坚果云,供自己和伙伴用任何设备访问.自上线以来,几乎所有用户都 ...

  4. wzplayer for android V1.5.3 (新增YUV文件播放)

    wzplayer for android V1.5.3 新增功能 1.使用gl es2 播放 yuv 文件. 联系方式:weinyzhou86@gmail.com QQ:514540005 版权所有, ...

  5. Android开发之PackageManager类

    PackageManger,可以获取到手机上所有的App,并可以获取到每个App中清单文件的所有内容. 设置应用程序版本号在应用程序的manifest文件中定义应用程序版本信息.2个必须同时定义的属性 ...

  6. WordPress Kernel Theme ‘upload-handler.php’任意文件上传漏洞

    漏洞名称: WordPress Kernel Theme ‘upload-handler.php’任意文件上传漏洞 CNNVD编号: CNNVD-201311-127 发布时间: 2013-11-12 ...

  7. 浅谈 HTML5 的 DOM Storage 机制 (转)

    在开发 Web 应用时,开发者有时需要在本地存储数据.当前浏览器支持 cookie 存储,但其大小有 4KB 的限制.这对于一些 Ajax 应用来说是不够的.更多的存储空间需要浏览器本身或是插件的支持 ...

  8. 【转】简单几步让App Store软件下载快如迅雷 -- 不错!!!

    原文网址:http://pad.zol.com.cn/237/2376160_all.html 下载速度慢的原因     1)国内用户从苹果软件商店下载软件速度很慢这是大家都知道的事实,究其原因就是苹 ...

  9. sharepoint2010网站根据权限隐藏ribbon

    转:http://www.it165.net/design/html/201302/1734.html 项目要求让普通用户看不到"网站操作",为了解决该问题,我找了好几篇博客,但都 ...

  10. lightoj 1012

    水题,dfs #include<cstdio> #include<string> #include<cstring> #include<iostream> ...