Get this question,
The first reaction is to use the algorithm idea of divide and conquer, and put the biggest piece of pancake at the specified position each time.
, the idea is simple and easy to implement. But this just provides a workable solution to the problem, after reading the contents of the book found that the topic is the most optimized output process, our method obviously does not take into account the optimization <-_->!!
In fact, I think even if I see the most optimal output of the requirements, it is estimated that the design ideas in the book. After a while, wait for yourself to forget the ideas in the book, and then see if you can think of this algorithm thought. This is supposed to be a pit, <-_->!!.
Here, put your own code on it:
//================ "pie problem" =================//@ author:zhyh2010//@ date:20150611//@ version:1.0//@ Description: required to ensure that the incoming is the ID of the real sequence, ie subscript starting from 1 calculation//program internal automatically converted to subscript 0 to start the calculation//=============== "End flipping problem" =================#include <stdio.h>#include <stdlib.h>#include <time.h>#define NUMintArr[num] = {0};intFlip_num =0;voidInit () {time_t TM; Time (&TM); Srand ((unsigned int) tm); for(inti =0; I! = NUM; i++) Arr[i] = rand ()% -;}voidDisplay () { for(inti =0; I! = NUM; i++)printf("%5d", Arr[i]);printf("\ n");}intGetmaxpie (intN_max) {intId_max =0; for(inti =0; I! = N_max; i++) {if(Arr[id_max] < arr[i]) Id_max = i; }//required to ensure that the incoming is the real sequence ID, ie subscript starting from 1 calculation returnId_max +1;}voidSwapint& A,int& B) {A = a ^ b; b = a ^ b; A = a ^ b;}voidFlipintID) {flip_num++;intLow =0;intHigh = ID-1; while(Low < High) swap (arr[low++], arr[high--]);printf("Post%d rollover result: \n\t", Flip_num); Display ();}voidFlipsort (intN) {if(n = =1)return;intid = getmaxpie (n);if(id! =1) Flip (ID); Flip (n); Flipsort (N-1);}voidMain () {init ();printf("Original Data are:\n\t"); Display (); Flipsort (NUM);printf("total rollover%d times \ n", flip_num);}
One of the beauty of programming stacks of pancakes sort 1