zoukankan      html  css  js  c++  java
  • Uva 110

    题目来源: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 $ leq$ n $ leq$ 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
    ", 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(")
    ");
    25             printf("else
    ");
    26         }
    27         else {
    28             dfs(d + 1);
    29             printf("else
    ");
    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(")
    ");
    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);
    var
    ");
    55         printf("a");
    56         for (int i = 2; i <= n; i++)
    57             printf(",%c", i + 'a' - 1);
    58         printf(" : integer;
    begin
    readln(");
    59         printf("a");
    60         for (int i = 2; i <= n; i++)
    61             printf(",%c", i + 'a' - 1);
    62         printf(");
    ");
    63         dfs(0);                //开始深搜
    64         printf("end.
    ");
    65         if (t != 0)
    66             printf("
    ");
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    mysql dump 完全备
    CentOS7位安装MySql教程
    mysql 数据备份
    Linux下iostat命令详解
    端口映射
    git+jenkins jar包代码的发布加新建项目
    腾讯面试题
    PHP-----类与对象,成员方法,成员属性,构造方法,析构方法
    PHP-----二维数组和二分查找
    PHP-----数组和常见排序算法
  • 原文地址:https://www.cnblogs.com/zpfbuaa/p/5049541.html
Copyright © 2011-2022 走看看