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 模仿 ...
随机推荐
- Design Tutorial: Learn from Life
Codeforces Round #270 B:http://codeforces.com/contest/472/problem/B 题意:n个人在1楼,想要做电梯上楼,只有1个电梯,每次只能运k个 ...
- ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- 点点滴滴-NET下的常用框架
刘冬的博客:http://www.cnblogs.com/GoodHelper/category/214139.html (Spring.net和Nhibernate) Kyo-yo : http: ...
- 【Python】代码行数统计
两级目录,可扩展为N级. # Count the line of dir or file import os, fnmatch, fileinput def ChkFileType(lst): tmp ...
- 【HDOJ】1033 Edge
题目英文太多,简单翻译了一下:1. For products that are wrapped in small packings it is necessary that the sheet of ...
- hdu4649Professor Tian
http://acm.hdu.edu.cn/showproblem.php?pid=4649 枚举当前状态是0或者是1的概率 其实就枚举1的概率 最后概率乘这个数 位运算用得不熟 老出错 #incl ...
- hdu4671Backup Plan
http://acm.hdu.edu.cn/showproblem.php?pid=4671 这个高端的题意啊 看了N久啊 n>m时 直接第一列按顺序来 第二列为M+1 else 第一列顺序 ...
- 阿里巴巴SUI Mobile的使用
1.引入文件 <link rel="stylesheet" href="./css/sm.min.css"> <link rel=" ...
- [HDU 1011] Starship Troopers
Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [HDU 5029] Relief grain
Relief grain Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)T ...