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

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

 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 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.

Miguel Revilla 2001-05-25
 
推荐博客:http://www.cnblogs.com/java20130723/p/3212108.html
 
代码:(没有缩进处理)
 
 1 #include <cstdio>
 2 const int maxn = 10;
 3
 4 int n, arr[maxn];
 5
 6 void insert_sort(int p, int c) {        //插入排序
 7     for (int i = c; i > p; i--)
 8         arr[i] = arr[i - 1];
 9     arr[p] = c;
10 }
11
12 int dfs(int d) {
13     int tmp[d + 1];                        //创建数组储存原来的数值,不然会乱掉
14     for (int j = 1; j <= n; j++)
15         tmp[j] = arr[j];
16     for (int i = d; i >= 1; i--) {            //循环从现排好的串后序进行dfs
17         printf("if %c < %c then\n", arr[i] + ‘a‘ - 1, d + ‘a‘);
18         insert_sort(i + 1, d + 1);            //将接下去的字母插入到i+1的位置
19         if (d + 1 == n) {                    //dfs到最深处,输出
20             printf("writeln(");
21             printf("%c", arr[1] + ‘a‘ - 1);
22             for (int j = 2; j <= d + 1; j++)
23                 printf(",%c", arr[j] + ‘a‘ - 1);
24             printf(")\n");
25             printf("else\n");
26         }
27         else {
28             dfs(d + 1);
29             printf("else\n");
30         }
31         for (int j = 1; j <= n; j++)        //还原数组
32             arr[j] = tmp[j];
33     }
34     insert_sort(1, d + 1);                    //下面是对最后一个情况,即字母插到整个数组前面,这里是没有else的
35     if (d + 1 == n) {
36         printf("writeln(");
37         printf("%c", arr[1] + ‘a‘ - 1);
38         for (int j = 2; j <= d + 1; j++)
39             printf(",%c", arr[j] + ‘a‘ - 1);
40         printf(")\n");
41     }
42     else
43         dfs(d + 1);
44     for (int i = 1; i <= n; i++)
45         arr[i] = tmp[i];
46 }
47
48 int main() {
49     int t;
50     scanf("%d", &t);
51     while (t--) {
52         scanf("%d", &n);
53         //前面部分
54         printf("program sort(input,output);\nvar\n");
55         printf("a");
56         for (int i = 2; i <= n; i++)
57             printf(",%c", i + ‘a‘ - 1);
58         printf(" : integer;\nbegin\nreadln(");
59         printf("a");
60         for (int i = 2; i <= n; i++)
61             printf(",%c", i + ‘a‘ - 1);
62         printf(");\n");
63         dfs(0);                //开始深搜
64         printf("end.\n");
65         if (t != 0)
66             printf("\n");
67     }
68     return 0;
69 }
时间: 2024-12-23 11:11:58

Uva 110 - Meta-Loopless Sorts(!循环,回溯!)的相关文章

uva 639 Don&#39;t Get Rooked ( 回溯 )

这道题确实是标准的回溯,果然还是早上比较清醒一些,昨天晚上想了好长一会儿都没有想起来,早上一会的功夫就A 了,估计也有昨天晚上的帮助...总感觉不想写太多私人的东西在这上面,因为这个是每个人都可以无条件访问的... 思路: 由于数据比较小,可以把每个元素都遍历一遍,回溯选择,最多4*4,还是很小的,我交的才1ms,1A.. 贴代码: #include<stdio.h> #include<string.h> #include<stdlib.h> int map[10][1

UVa 110 没有循环的排序程序

题意:构造Pascal的排序程序.初看是写Pascal程序,不了解的以为会很难,但其实程序的大部分是固定的,直接printf就可以,主要在于写比较的if-else部分. 思路:看sample out可以大概知道程序的构成,其他部分直接输出,主要写比较的部分.比较的时候,可以看成两个集合,A是已排好序的,S是全集,cur是从左到右扫描S的当前位置.用递归写的,前半部分是当当前位置cur到达n时,即可以输出了:剩下的是如何填写当前位置cur以及维持A是已排好序的这个性质.将新元素S[cur]从A的最

UVa 524 Prime Ring Problem【回溯】

题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<m

UVA - 110

 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

UVa 524 Prime Ring Problem (回溯)

第一次无指导写的回溯. 感觉也不难,小紫书上说"学习回溯短则数天,长则数月或一年以上", 但我没用一小时就懂了回溯,不知道是真懂还是假懂. 这道题很简单.细心就好. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; int n, ans[25]; const int pn[] = {1,

uva 331 Mapping the Swaps (回溯)

uva 331 Mapping the Swaps Sorting an array can be done by swapping certain pairs of adjacent entries in the array. This is the fundamental technique used in the well-known bubble sort. If we list the identities of the pairs to be swapped, in the sequ

UVa 524 Prime Ring Problem(回溯法)

传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1, 2, . . . , n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle sho

【UVA】10318-Security Panel(DFS 回溯)

比较纠结的一道题,改了好几次才对. 首先需要知道的就是每个点如果重复点2次是没有任何意义的,所以一个点只有选或者不选,那么时间复杂度最大为2^25 毫无疑问超过了允许的范围,那么考虑减枝. 由于是3 X 3 的改变范围,所以如果 当前走到了第三行,那么就没办法更改第一行的状态,如果走到了第四行就无法更改第一 二行的状态,所以如果这个时候第一 二行 还没改成亮的它永远也不可能亮了. 最后一个就是需要输出按开关的次数最小的方案,一开始没看见,WA了好几次.. 14171735 10318 Secur

UVA - 524 Prime Ring Problem(dfs回溯法) 解题心得

原题 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1.