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. POJ3714+最近点对

    特判标记即可 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h& ...

  2. Android 国际化字符串资源,各语言目录表及从字符串资源文件读字符方法

    字符串国际化 只要在 res 目录下 建对应语言的 values 目录即可. res下values是所有资源(包括字符串资源)的默认目录,如果没有特指资源目录或找不到某资源时,就用它. 如values ...

  3. keytool 错误: java.io.FileNotFoundException: 拒绝访问

    keytool 错误: java.io.FileNotFoundException: 拒绝访问 打开命令行,切换到D:\AndroidStudioProjects\MyApplication\app目 ...

  4. hdu1838Chessboard(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1838 其实原先不知道这题是DP 我都想不到DP去 想了想没思路 看了下题解 经典思路 :第n大的都是由第n-1大 ...

  5. URI Scheme

    1. 什么是URI Scheme? 一般情况下,遇到这种概念不清的问题,最好的第一手资料就是wiki,实在看不懂,再看百度百科,但前者给出的资料一般都是更加准确一些. 以下为维基百科和百度百科关于这个 ...

  6. BZOJ_1013_[JSOI2008]_球形空间产生器_(高斯消元)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1013 n维空间,给出球上n+1个点的n维坐标,求球心坐标. 提示:给出两个定义:1. 球心:到 ...

  7. windows播放声音

    一般播放MP3常见的有两种方法,一种是自己解码,另外一种用系统的库,比如MCI,当然如果可以用控件直接用个控件会更方便. 1.      使用mci #include <windows.h> ...

  8. wildfly-9.0.2 web项目部署详细步骤

    一.配置操作系统环境变量 JAVA_HOME = C:\Program Files (x86)\Java\jdk1.7.0_67 JBOSS_HOME = F:\server\wildfly-9.0. ...

  9. 从零开始学习jQuery (十) jQueryUI常用功能实战

    一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案,  即使你会使用jQuery也能在阅读中发现些许秘籍. 本文是实战篇. 使用jQueryUI完成制作网站 ...

  10. android webview 访问https页面 SslError 处理

    在Android中,WebView可以用来加载http和https网页到本地应用的控件.但是在默认情况下,通过loadUrl(String url)方法,可以顺利loadUrl(“http://www ...