Starting today at least one algorithm per day.
Objective
The question of flipping pancakes is a very classic question, and in the evening of Friday, a bunch of colleagues drank a few more cups in the hard drive bar near the Sigma building. What does the programmer talk about after drinking a few more cups? Nature is an algorithmic problem. One colleague said:
"I used to work in restaurants and customers often order very many pancakes. The size of the cake in the shop is different, I used to arrive at the Customer table, put a stack of cakes in the order of size-small in the top, big under. As I was holding the plate in one hand, I had to use the other hand, grabbed the top of the cake at once, and turned them upside down, and repeated several times, and the stacks of pancakes were lined up.
I thought later that this was actually an interesting sort of problem: suppose there were n blocks of different sizes of pancakes, which would have to be doubled at least several times to achieve the final size and order of the results?
package z_fanlaobing;/** * Programming Beauty: Flipping pancakes * * @author jixiang * @date 2018/3/6 * */public class fanlaobing { public Static void main (String[] args) { int n=10; int[] num={2,5,4,9,8,3,1,6,0,7}; int max=num[0]; int maxid=0;//of the largest pancake for (int i=0;i<n;i++) { for (int j=i+1;j<n;j++) { if (Num[j]>max) { Num[j]=max; maxid=j; } } reverse (NUM,MAXID);//Find the largest one before starting to turn the biggest to the top reverse (num,n-i-1);//Then the whole is not lined up, let the biggest turn to the bottom. } for (int k=0;k<10;k++) { System.out.print (k); } } public static void reverse (int[] num,int n) { int i=0; &nbSp; int temp; if (n>num.length) { system.out.println ("exceeds array size"); return; } while (i<n) { temp=num[i]; num[i]= num[n]; num[n]=temp; n--; i++; } }}
Beauty of programming-flipping pancakes Java implementation