翻饼的问题,纯模拟
#include <stdio.h>
#include <stdlib.h>
int data[100], t[100], num[100];
int n;
int cmp(const void *a, const void *b)
{
return *(int *)a-*(int *)b;
}
void reverse(int m)
{
int mid=(m+1)>>1, t;
for ( int i = 0 ; i < mid ; i++ )
{
t= data[i];
data[i]= data[m-i];
data[m-i]= t;
}
}
int main()
{
while ( scanf("%d", &n), n)
{
int i, j, p, count= 0;
for ( i = 0 ; i < n ; i++ )
{
scanf("%d", data+i);
t[i]= data[i];
}
qsort(t, n, sizeof(int), cmp);
for ( p = n-1 ; p >= 0 ; p-- )
{
if ( data[p] != t[p] )
{
for ( i = 0 ; i < n && t[p] != data[i] ; i++ );
if ( p > 0 )
{
for ( j = 0 ; j < n && t[p-1] != data[j] ; j++ );
if ( j+1 < i )
{
if ( 0 < j )
{
reverse(j);
num[count++]=j;
}
reverse(i-1);
num[count++]= i-1;
}
}
if (i)
{
reverse(i);
num[count++]= i;
}
reverse(p);
num[count++]= p;
}
}
printf("%d", count);
for ( i = 0 ; i < count ; i++ )
printf(" %d", num[i]+1);
putchar(10);
}
return 0;
}