Pay attention to the following points:
1. Pie turning rules: You can only flip from the bottom layer, that is, reverse the first I element. So here is a tip, that is, find the largest element in the first I element (assuming the first m), and turn it to the first layer (the first m element reverse ), then go to layer I (the first I element reverse) (the output n-I, not I)
2. The input line must be output.
[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Int max (int a [], int n)
{
Int I, max = a [0];
For (I = 1; I <n; I ++)
{
If (a [I]> max)
Max = a [I];
}
Return max;
}
Int find (int a [], int n, int find)
{
Int I;
For (I = 0; I <n; I ++)
{
If (a [I] = find)
Break;
}
Return I;
}
Void flap (int l, int r, int a [])
{
For (int j = l; j <r/2; j ++)
{
Int temp = a [j];
A [j] = a [r-1-j];
A [r-1-j] = temp;
}
}
Int main ()
{
Int a [40], flag, f, m, count = 0;
Char t;
While (scanf ("% d % c", & a [count], & t) = 2)
{
Count ++;
If (t = '\ n ')
{
For (int I = 0; I <count; I ++)
{
If (! I)
Printf ("% d", a [I]);
Else
Printf ("% d", a [I]);
}
Printf ("\ n ");
For (int I = 0; I <count; I ++)
{
If (! I)
{
Flag = 0;
For (int j = 0; j <count-1; j ++)
{
If (a [j]> a [j + 1])
Flag ++;
}
If (flag = 0)
{
Printf ("0 \ n ");
Memset (a, 0, count );
Count = 0;
Break;
}
}
M = max (a, count-I );
F = find (a, count-I, m );
If (! F)
{
Flap (0, count-I, );
Printf ("% d", I + 1 );
}
Else if (f! = Count-i-1)
{
Flap (0, f + 1, );
Printf ("% d", count-f );
Flap (0, count-I, );
Printf ("% d", I + 1 );
}
If (I)
{
Flag = 0;
For (int j = 0; j <count-1; j ++)
{
If (a [j]> a [j + 1])
Flag ++;
}
If (! Flag)
{
Printf ("0 \ n ");
Memset (a, 0, sizeof ());
Count = 0;
Break;
}
}
}
}
}
Return 0;
}