码迷,mamicode.com
首页 > Web开发 > 详细

UVA110 Meta-Loopless Sorts【暴力】

时间:2019-03-02 01:02:40      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:name   generate   const   world   forms   compile   port   链接   char   

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 is to create several programs whose output is a standard Pascal programs that sorts n numbers where n is the only input to the program you will write. The Pascal program 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
????????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.
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.
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.

问题链接UVA110 Meta-Loopless Sorts
问题简述:(略)
问题分析
????代码生成和格式问题,比较繁琐。
????用递归实现可以变得简单一些。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++程序如下:

/* UVA110 Meta-Loopless Sorts */

#include <bits/stdc++.h>

using namespace std;

const int N = 8;
char s[] = "abcdefgh";
int a[] = {0, 1, 2, 3, 4, 5, 6, 7};
int n;

void outputs(int k)
{
    for(int i = 0; i < k; i++) {
        if(i != 0)
            printf(",");
        printf("%c", s[a[i]]);
    }
}

void sort(int k)
{
    if(k == n) { //输出并返回
        for(int i = 0; i < k; i++)
            printf("  ");
        printf("writeln(");
        outputs(k);
        printf(")\n");
    } else {
        int b[N];
        memcpy(b, a, sizeof(a));        // 备份

        a[k] = k;
        if(k == 0) sort(k + 1);

        for(int i = 0; i < k; i++)
            printf("  ");
        for(int i = k - 1; i >= 0; i--) {
            printf("if %c < %c then\n", s[a[i]], s[a[i + 1]]);
            sort(k + 1);

            for(int j = 0; j < k; j++)
                printf("  ");
            if(i) printf("else ");
            else printf("else\n");

            swap(a[i], a[i + 1]);
            if(i == 0) sort(k + 1);
        }

        memcpy(a, b, sizeof(a));        // 还原
    }
}

int main()
{
    int m;
    scanf("%d", &m);
    while(m--) {
        scanf("%d", &n);
        printf("program sort(input,output);\nvar\n");
        outputs(n);
        printf(" : integer;\nbegin\n  readln(");
        outputs(n);
        printf(");\n");
        sort(0);
        printf("end.\n");
        if(m) printf("\n");
    }

    return 0;
}

UVA110 Meta-Loopless Sorts【暴力】

标签:name   generate   const   world   forms   compile   port   链接   char   

原文地址:https://www.cnblogs.com/tigerisland45/p/10459434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!