心如止水
Je n'ai pas le temps
posts - 400,comments - 130,trackbacks - 0
题目大意:通过一系列的反转操作,给一个序列排序。
类似于选择排序的思路,每次找当前序列的最大值放在合适的位置即可。
以下是我的代码:
#include<stdio.h>
#include
<string.h>
void deal(char *s,long *a,long &n)
{
    
long begin,end,len=strlen(s);
    n
=0;begin=0,end=begin;
    
while(end<len)
    {
       
while(s[end]!=' '&&s[end]!=0) end++;
       
long t=0;
       
while(begin<end)
       {
          t
*=10;
          t
+=s[begin]-'0';
          begin
++;
       }
       n
++;a[n]=t;
       begin
=end+1;end=begin;
    }
}
void flip(long *a,long n,long pos)
{
    
long begin=pos,end=n;
    
while(begin<=end)
    {
       
long t=a[begin];a[begin]=a[end];a[end]=t;
       begin
++;end--;
    }
}
int main()
{
    
/*
    freopen("data.in","r",stdin);
    freopen("data.out","w",stdout);
    //
*/
    
const long maxn=37;
    
char s[maxn*10];
    
while(gets(s)!=NULL)
    {
       
long n=0,a[maxn],tmp[maxn];
       deal(s,a,n);
       
for(long i=1;i<=n;i++) tmp[n-i+1]=a[i];
       
//  Init
       for(long i=1,first=1;i<=n;i++)
       {
          
if(first) first=0;
          
else putchar(' ');
          printf(
"%ld",a[i]);
       }
       putchar(
'\n');
       
//  First Line
       for(long i=1;i<=n;i++)
       {
          
long max=i;
          
for(long j=n;j>i;j--)
            
if(tmp[max]<tmp[j]) max=j;
          
if(max!=i)
          {
             
if(max!=n)
             {
                flip(tmp,n,max);
                printf(
"%ld ",max);
             }
             
if(max!=i)
             {
                flip(tmp,n,i);
                printf(
"%ld ",i);
             }
          }
       }
       printf(
"%ld\n",0);
    }
return 0;
}


posted on 2010-02-05 11:22 lee1r 阅读(1386) 评论(1)  编辑 收藏 引用 所属分类: 题目分类:基础/模拟

FeedBack:
# re: UVa 120 Stacks of Flapjacks
2011-04-27 15:50 | sai
嘿嘿。。。这题我也过了。看错题了=  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理