UVA - 120

UVA - 120 博客第一题就给这个吧,模拟一下紫书236p的思路,第一个是自己写的好麻烦,另一个是别人写的用了一些stl的东西,代码简短,理解完STL代码后可以用第一种方式写写,可以当一个很好的模拟题。

#include <iostream>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cstring>  
#include <string>  
#include <sstream>  
#include <vector>  
#include <set>  
#include <map>  
#include <algorithm>  
#include <stack>  
#include <queue>  
#include <bitset>   
#include <cassert>   
  
using namespace std;  
typedef long long ll;
int a[10550];
int b[15050];
int cmp(int q,int w)
{
return q>w;
}
int ans[105];


int main()
{
string line;
while(getline(cin,line))
{
int m=0;
int temp,n=0;
stringstream ss(line);
while(ss>>temp)
{
n++;
b[n]=temp;
a[n]=temp;
}
for(int i=1;i<n;i++)
{
printf("%d ",a[i]);
}printf("%d\n",a[n]);

sort(b+1,b+n+1,cmp);

while(1)
{
int x;
int flag=1;
int fff;
for(int j=n,k=1;j>=1;k++,j--)
{
if(a[j]!=b[k]){x=k;fff=j;flag=0;break;}//如果从右向左a的第j位和第j大不相等
}
//printf("-------%d\n",x);
if(flag==1)break;  //调换完毕 

int ff=x;

for(int j=1;j<=n;j++)
{
if(a[j]==b[ff])
{
x=j;break;
}
}

if(x!=1) //如果在第一位直接交换到应该在的位置 ,如果不在则应该把它先调到第一位 
{
ans[m++]=(n-x+1);
for(int i=1,j=x;i<j;i++,j--)
swap(a[i],a[j]);
}

ans[m++]=n-fff+1;
for(int i=1,j=fff;i<j;i++,j--)
swap(a[i],a[j]);

/* for(int i=1;i<=n;i++)
{
printf("%d ",a[i]);
}cout<<endl;*/
}
for(int i=0;i<m;i++)
{
printf("%d ",ans[i]);
}
printf("0\n");
}
 
return 0;
}



STL实现

#include <stdio.h>
#include<string.h>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int main()
{
for(string line;getline(cin,line);cout<<0<<endl)
{
deque<int> q;
int x;
int xxx=0;
stringstream ss(line);
while(ss>>x)
{
if(xxx==0)
{
cout<<x;
xxx=1; 
}
else cout<<" "<<x;
q.push_front(x);
}
cout<<endl;

deque<int>::iterator it;
for(it=q.begin();it!=q.end();it++)
{
deque<int>::iterator max_pos;
max_pos=max_element(it,q.end());
if(it!=max_pos)
{
if(max_pos!=q.end()-1)
{
cout<<distance(q.begin(),max_pos)+1<<" ";
reverse(max_pos,q.end());
}
cout<<distance(q.begin(),it)+1<<" ";
reverse(it,q.end());
} 
} 
} 
return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值