UVa 110 No loop sequencing program

Source: Internet
Author: User

Test instructions: Constructs a sort program for Pascal. The first look is to write Pascal program, do not understand the thought will be difficult, but in fact, most of the program is fixed, direct printf can be, mainly in the If-else part of the written comparison.

Idea: See sample out can probably know the composition of the program, the other part of the direct output, mainly write the comparison of the part. When comparing, it can be regarded as two sets, a is already ordered, S is the complete set, cur is the current position of scanning s from left to right. With recursion, the first half is when the current position cur arrives n, that is, it can be output, and the rest is how to fill in the current position cur and maintain a is the order of this nature. Compare the new element S[cur] from the rightmost end of a to the left, and if the new element S[cur] is larger, place it directly on the right side of the current element of a i+1 position; otherwise, the new element is smaller, moving the current element of a to the right one bit, that is, to the i+1 position, and the new element continues to compare to the left Another thing is that because the array will change content after it is passed to the function, a new array B is constructed before each function, and the contents of the a array are copied and then output. Otherwise, do not use B array, directly pass a array, for two elements no problem, for three elements, in the IF a<b this part is correct, and in this condition open else inside will find no A, and two C, this is because the recursive although returned, but the array A has been modified, The first bit A has been modified to C. In addition, the topic did not say indentation requirements, sure enough to find that each line of indentation is not required, as long as the guarantee Writeln statement alone in a row.

Locally modified several times, once AC, get the highest current of all topics ranked 74, before preferably 11234 row 79. is to do too slow, think for a long time, and began to consider the problem of indentation, after all, sample out of the first If-else and later If-else indentation is different ...

Code:

#include <stdio.h> #include <string.h>void solve (int n), void compare (int n,char *a,char *s,int cur), char str[ ]= "ABCDEFGHIJKLMN";  int main () {int m;  scanf ("%d", &m);    while (m-->0) {int n;    scanf ("%d", &n);    Solve (n);  if (m) printf ("\ n");  } return 0;  }void solve (int n) {printf ("program sort (input,output); \ n");  printf ("var\n");  for (int i=0;i<n-1;++i) printf ("%c,", Str[i]);  printf ("%c:integer;\n", str[n-1]);  printf ("begin\n");  printf ("Readln (");  for (int i=0;i<n-1;++i) printf ("%c,", Str[i]);  printf ("%c"; \ n ", str[n-1]);  Char a[10];  Compare (n,a,str,0); printf ("end.\n");}    void Compare (int n,char *a,char *s,int cur) {if (cur==n) {printf ("writeln (");    for (int i=0;i<cur-1;++i) printf ("%c,", A[i]);    printf ("%c" \ n ", a[cur-1]);          return;    } for (int i=cur-1;i>=0;--i) {printf ("If%c <%c then\n", A[i],s[cur]);    A[i+1]=s[cur];    Char b[10];    strcpy (B,a);    Compare (n,b,s,cur+1);    printf ("else\n"); A[i+1]=a[i];   A[i]=s[cur];  Compare (n,a,s,cur+1);  } a[0]=s[cur];//initial cur=0 char b[10];  strcpy (B,a); Compare (n,b,s,cur+1);}


UVa 110 No loop sequencing program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.