120 - Stacks of Flapjacks
Solution Description : Ad hoc simulation If you do not understand the flip then see the example 1 5 4 3 1 2 flip(2) 3 flip(1) 1 flip(2) 2 flip(3) 2 3 ------> 2 ------> 2 ------> 1 ------> 3 5 1 3 4 4 4 4 5 5 5 I think now you can understand the flip. For solve this problem i use an integer array A[31] Input a line as string and split the string using strtok() method and convert all value to integer and store all value to array A[] Now you need to sort the array from small to greater Now find the position maximum value from array --If the position is 1 then flip(1) here you can set the position of first maximum value --if the position is n then do nothing --if the position between 2 and n-1 then flip(n-position) and also flip(1) here you can set the position of first maximum value This process do until the array A[] are sorted |
||||||||||