Beauty of programming-flipping pancakes Java implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.