Test instructions
Gives an arrangement of 1 to N, giving the order of operations so that ascending order can become the given arrangement.
Analysis:
The idea of a normal bubbling sort. If the first two numbers, the front is greater than the back, then the change (special case is n,1 cannot be swapped). Otherwise, use 2 inverse operation, put the last number in front. But the vector array was used to store
Code:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace Std;
int n;
Vector<int> A,ans;
int main ()
{
int i,j;
while (~SCANF ("%d", &n) &&n)
{
A.clear ();
Ans.clear ();
for (int i=0;i<n;++i)
{
int k;
scanf ("%d", &k);
A.push_back (k);
}
while (1)
{
if (a[0]==1)
{
BOOL Ok=true;
for (I=0;i<n;++i)
if (a[i]!=i+1)
{
Ok=false;
Break
}
if (OK)
Break
}
if (a[0]<a[1]| | (a[1]==1&&a[0]==n))
{
Ans.push_back (2);
A.insert (A.begin (), a[n-1]);
A.erase (A.end ()-1);
}
Else
{
Ans.push_back (1);
Swap (a[0],a[1]);
}
}
For (I=ans.size () -1;i>=0;--i)
printf ("%d", ans[i]);
printf ("\ n");
}
return 0;
}
UVA 11925-generating permutations