uva 110 - Meta-Loopless Sorts

这道题一开始也是不知道怎么下手,本来想看别人的代码的,不过都很长,实在是看不下去,后来注意到了一点,就是整个过程都是“<”小于号,又或者整个过程都是大于号,这个举动给了我思路,就是和第一个比,比它大就排在他的前面,比它小就和第二个比,以此类推,用递归写,还用了链表,插入删除操作比较简单,估计能用数组的,还能省时间的,只是还没有思路。

#include<stdio.h>


typedef struct Node{
    char ch;
    Node *pre,*next;
}Node;


Node *head;
int n;


void print_space(int n){
    int i;
     for(i=0;i<n;i++)printf(" ");
}


void metasort(int ly,Node *p){
    int i;
    Node *tmp,*next,*pre;
    if(ly>n)return;
    if(ly==n){
        print_space(ly*2);
        tmp=head->next;
        printf("writeln(%c",tmp->ch);
        while(tmp->next!=NULL){
            tmp=tmp->next;
            printf(",%c",tmp->ch);
        }
        printf(")\n");
    }
    else{
        print_space(ly*2);
        if(p==head){
            printf("else \n");
        }
        else if(p->next==NULL){
            printf("if %c < %c then\n",p->ch,ly+'a');
        }
        else if(p->next!=NULL){
            printf("else if %c < %c then\n",p->ch,ly+'a');
        }
        tmp=new Node();
        tmp->next=NULL;
        tmp->ch=ly+'a';
        next=p->next;
        pre=p->pre;
        if(p->next!=NULL){
            p->next->pre=tmp;
            tmp->next=p->next;
        }
        p->next=tmp;
        tmp->pre=p;
        while(tmp->next!=NULL)tmp=tmp->next;
        metasort(ly+1,tmp);


        p->next=next;
        if(next!=NULL) next->pre=p;
        p->pre=pre;
        if(pre!=NULL) pre->next=p;
        if(p->pre!=NULL) metasort(ly,p->pre);
    }
}


main()
{
    int M,k=0;
    Node *q;
    head=new Node();
    head->ch='h';
    head->pre=NULL;
    scanf("%d",&M);
    while(M--){
        scanf("%d",&n);
        printf("program sort(input,output);\nvar\na");
        for(k=1;k<n;k++) printf(",%c",'a'+k);
        printf(" : integer;\nbegin\n  readln(a");
        for(k=1;k<n;k++) printf(",%c",'a'+k);
        printf(");\n");
        q=new Node();
        q->ch='a';
        q->next=NULL;
        q->pre=head;
        head->next=q;
        metasort(1,q);
        printf("end.\n");
        if(M)printf("\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值