The 1th chapter of the game-the sort of a stack of pancakes

Source: Internet
Author: User

Sort of a stack of pancakes

Question: Write a program that outputs the most optimized cookie-making process for n-size pancakes? Make sure the pancakes are placed in the order of size-small on top, big underneath.

Analysis and Solution:

The code implemented in Java is as follows:
 Packagechapter1youxizhileprefixsorting;ImportJava.util.Scanner;/*** A stack of pancakes *@authorDELL **/ Public classprefixsorting {Private int[] M_cakearray;//Pancake Information Array    Private intm_ncakecnt;//Number of Pancakes    Private intM_nmaxswap;//maximum number of exchanges, according to inference, up to (m_ncakecnt-1) * *;    Private int[] M_swaparray;//array of interchange results    Private int[] M_reversecakearray;//array of currently flipped pancake information    Private int[] m_reversecakearrayswap;//Current flipping pancake swap result array    Private intM_nsearch;//Current number of search information         PublicPrefixsorting (int[] Pcakearray,intncakecnt) {m_ncakecnt= 0; M_nmaxswap= 0;    Run (Pcakearray, ncakecnt); }        /*** Calculate pancake Rollover Information *@paramPcakearray Storage Pancake Index Array *@paramncakecnt Number of pancakes*/     Public voidRun (int[] Pcakearray,intncakecnt)        {Init (Pcakearray, ncakecnt); M_nsearch= 0; Search (0); }        //output Pancake specific rollover times     Public voidOutput () {System.out.print ("The interchange result array is:");  for(inti=0;i<m_nmaxswap;i++) {System.out.print (M_swaparray[i]+" "); } System.out.println ("\n| Search times| : "+M_nsearch); System.out.println ("Total Swap times =" +M_nmaxswap); }        /*** Initialization of array information *@paramPcakearray Storage Pancake Index Array *@paramncakecnt Number of pancakes*/    Private voidInit (int[] Pcakearray,intncakecnt) {        if(pcakearray==NULL) System.out.println ("Pancake index array is empty!" "); if(ncakecnt <= 0) System.out.println ("The number of pancakes is illegal!" "); M_ncakecnt=ncakecnt; //Initialize the pancake arrayM_cakearray =New int[m_ncakecnt];  for(inti=0;i<m_ncakecnt;i++) {M_cakearray[i]=Pcakearray[i]; }        //set the maximum number of interchange informationM_nmaxswap =Upbound (m_ncakecnt); //initializing an array of interchange resultsM_swaparray =New int[M_nmaxswap]; //Initializing intermediate interchange result informationM_reversecakearray =New int[m_ncakecnt];  for(inti=0;i<m_ncakecnt;i++) {M_reversecakearray[i]=M_cakearray[i]; } M_reversecakearrayswap=New int[M_nmaxswap]; }        /*** Find the upper bound of the current rollover *@paramncakecnt Number of pancakes *@return     */    Private intUpbound (intncakecnt) {        return(nCakeCnt-1) * *; }        /*** Find the lower bound of the current flip *@paramPcakearray Current Rollover pancake Information array *@paramncakecnt Current number of pancakes *@return     */    Private intLowerbound (int[] Pcakearray,intncakecnt) {        intT, ret = 0; //determines the minimum number of times to be exchanged based on the sorting information of the current array         for(inti=1;i<ncakecnt;i++){            //determine the location of the adjacent two pancakes, whether the size of the sorting on the adjacentt = pcakearray[i]-pcakearray[i-1]; if(Math.Abs (t) ==1){                //dimension sort on adjacent count}Else{ret++; }        }        returnret; }        //sort the main function    Private voidSearch (intStep) {        intI, nestimate; M_nsearch++; //estimate the minimum number of exchanges required for this searchNestimate =lowerbound (M_reversecakearray, m_ncakecnt); if(Step + nestimate >M_nmaxswap)return; //if the order is already sorted, that is, the rollover is complete, the output        if(issorted (M_reversecakearray, m_ncakecnt)) {if(step<M_nmaxswap) {M_nmaxswap=step;  for(i=0;i<m_nmaxswap;i++) M_swaparray[i]=M_reversecakearrayswap[i]; }            return; }        //recursion to flip         for(i=1;i<m_ncakecnt;i++) {Revert (0, i); if(step<m_nmaxswap) M_reversecakearrayswap[step]=i; Search (Step+1); Revert (0, i); }    }        /*** Determine if the order is orderly *@paramPcakearray Current Rollover pancake Information array *@paramncakecnt Number of pancakes *@return     */    Private BooleanIsSorted (int[] Pcakearray,intncakecnt) {         for(inti=1;i<ncakecnt;i++){            if(Pcakearray[i-1] >Pcakearray[i]) {                return false; }        }        return true; }        /*** Flip Pancake Info *@paramnbegin Start Position *@paramNend End Position*/    Private voidRevert (intNbegin,intnend) {        if(nbegin>=nend) {System.out.println ("The parameter information is wrong!" The start position cannot be greater than or equal to the end position! "); }        inti,j,t; //Flip Pancake Info         for(I=nbegin,j=nend; i<j; i++,j--) {T=M_reversecakearray[i]; M_reversecakearray[i]=M_reversecakearray[j]; M_REVERSECAKEARRAY[J]=T; }    }         Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); System.out.println ("Please enter the radius from top to bottom of the pancake, separated by commas:"); String s=Input.nextline (); string[] Ch= S.split (",");//int ncakecnt = ten;        intncakecnt =ch.length; int[] Pcakearray =New int[ncakecnt];  for(inti=0;i<ncakecnt;i++) {Pcakearray[i]=Integer.parseint (Ch[i]); }//int[] Pcakearray = {3,2,1,6,5,4,9,8,7,0};prefixsorting PS =Newprefixsorting (Pcakearray, ncakecnt); Ps.    Output (); }}

The results of the program run as follows:

Please enter the radius from top to bottom of the pancake, separated by commas:3,2,1,6,5,4,9,8,7,0 Interchange result array:4 8 6 8 4 9 | Search times| : 148015= 6

The 1th chapter of the game-the sort of a stack of pancakes

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.