uva-120 stack of flapjacks

//wa 一次   没考虑  1 1 1 1 1 的情况

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmp_int(const void *a,const void *b)
{
    return *(int *)b - *(int *)a;
}
int Find(int f,int num[],int n)
{
    int i;
    for(i = n-1;i >=0;i--)
     if(f == num[i]) return i+1;
}

void Flip(int num[],int n)
{
    int i,j,cnum[200];
    for(i = 0;i < n;i++) cnum[i] = num[i];
    qsort(cnum,n,sizeof(int),cmp_int);

    for(i = 0;i < n;i++){
        int location;
        location = Find(cnum[i],num,n);
        if((location == n - i) || (cnum[i] == num[n-i-1])) continue;
        else if(location != 1){
            printf("%d ",n - location + 1);
            int j;
            for(j = 0;j < location /2;j++ ){
                int temp;
                temp = num[j];
                num[j] = num[location - j - 1];
                num[location - j - 1] = temp;
            }

            printf("%d ",i + 1);
            for(j = 0;j < (n-i)/2;j++ ){
                int temp;
                temp = num[j];
                num[j] = num[n-i-j-1];
                num[n-i-j-1] = temp;
            }
        }
        else{
            printf("%d ",i + 1);
            for(j = 0;j < (n-i)/2;j++ ){
                int temp;
                temp = num[j];
                num[j] = num[n-i-j-1];
                num[n-i-j-1] = temp;
            }
        }

        }
}
int to_num(char *s,int num[])  //在网上学的别人的代码
{
    int n = 0,begin = 0,end = 0,len = strlen(s);
    while(end < len){
        int t = 0;
        while(s[end]!=' '&& s[end] != 0) end++;
        while(begin < end){
            t*=10;
            t+=s[begin] - '0';
            begin++;
        }
        num[n++] = t;
        begin = end + 1;end = begin;
    }
    return n;
}
int main()
{
    int i,num[200];
    char a[200];
    while(gets(a)!=NULL){
        int n;
        printf("%s/n",a);
        n = to_num(a,num);
        Flip(num,n);
        printf("0/n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值