UVA - 110

本文介绍了一种名为元循环排序(Meta-LooplessSorts)的排序算法,该算法旨在生成针对特定数量输入的Pascal程序,这些程序能够对输入进行排序。文章详细描述了生成的排序程序必须遵循的规则,包括变量声明、读取输入、比较和输出语句的具体要求。此外,还提供了一个示例,展示了如何为三个输入生成排序程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5  
 6 using namespace std;
 7  
 8 const int MAXN = 10;
 9  
10 char str[MAXN];
11 int n;
12  
13 void space(int cnt) {
14     for(int i = 0; i < cnt; i++)  
15         printf("  ");  
16 }
17  
18 void deal(int cur, vector<char> &s) {
19     if (cur == n) {
20         space(n);
21         printf("writeln("); 
22         printf("%c", s[0]);
23         for (int i = 1; i < n; i++) 
24             printf(",%c", s[i]);
25         printf(")\n");
26         return;
27     }
28     else {
29         for (int i = cur; i >= 0; i--) {
30             space(cur);
31             if (i < cur) 
32                 printf("else");
33             if (i != cur) cout << " ";
34             if (i)
35                 printf("if %c < %c then", s[i - 1], cur + 'a');
36             printf("\n"); 
37             vector<char> ss(s); 
38             ss.insert(ss.begin() + i, 'a' + cur);
39             deal(cur + 1, ss);
40         }  
41     }  
42 }
43  
44 int main() {
45     int cas;
46     scanf("%d", &cas);
47     while (cas--) {
48         scanf("%d", &n);     
49         for (int i = 0; i < n; i++)
50             str[i] = 'a' + i;
51         printf("program sort(input,output);\nvar\n");
52         printf("%c", str[0]);
53         for (int i = 1; i < n; i++)
54             printf(",%c", str[i]);
55         printf(" : integer;\nbegin\n");
56         printf("  readln(%c", str[0]);
57         for (int i = 1; i < n; i++)
58             printf(",%c", str[i]);
59         printf(");\n");
60         vector<char> s;
61         s.push_back('a');
62         deal(1, s);
63         printf("end.\n");
64         if (cas)
65             printf("\n"); 
66     }
67     return 0;
68 }
View Code

 

转载于:https://www.cnblogs.com/xiaoshanshan/p/4127727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值