UVa120 WA,谢谢
题目的地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=3&problem=56&mosmsg=Submission+received+with+ID+12367536
题意:一摞盘子,从上到下。个数1=<N<=30,盘子直径D,1~100,整数且不重复。求:每次只能执行从顶部到某一个的反转,也就是像堆栈一样,先倒出来,再倒回去,最终实现,从顶到底有序(升序)。输出每次反转的位置(即每次反转的底层位置),编号从底到顶从1~N。
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int maxsize=50;
int find_max(int a[],int n)
{
int m=0;
int i;
for(i=1;i<n;++i)
if(a[i]>a[m])
m=i;
return m;
}
void exchange(int a[],int k)
{
int i,j;
for(i=0,j=k;i<j;++i,--j)
{
int x;
x=*(a+i);
*(a+i)=*(a+j);
*(a+j)=x;
}
}
int main()
{
int A[maxsize];
string s;
int n;
while(getline(cin,s))
{
istringstream input(s);
n=0;
while (input>>A[n++]);
--n;
int i;
int flag=0;
if(n==1)
{
cout<<0<<endl;
continue;
}
for(i=0;i<n-1;++i)
{
int num=find_max(A,n-i);
if(num==0)
{
cout<<i+1<<" ";
exchange(A,n-i-1);
}
else if(num==n-i-1)
continue;
else
{
cout<<n-num<<" "<<i+1<<" ";
exchange(A,num);
exchange(A,n-i-1);
}
}
cout<<0<<endl;
}
return 0;
}