Get this problem.
The first reaction is to use the algorithm idea to divide and conquer, each time the biggest pancake of the current to the designated place
。 The idea is simple and easy to implement. But this is just a workable solution to the problem, which is found after reading the contents of the book. The problem is that the optimization of the output process is required, and our approach obviously does not take into account the optimization of the <-_->!!
In fact, I think that even if I saw the demand for the most optimal output, I was not expected to expect the design idea in the book. After a while, wait for yourself to forget the ideas in the book. Let's see if we can think of such algorithmic thinking. 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 guarantee that the incoming is the ID in the real sequence. IE subscript is calculated from the beginning of 1//The program internal self-conversion into subscript for 0 start to calculate//=============== "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; }//require that the IDs in the real sequence be passed in. IE subscript is calculated from the beginning of 1 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);}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Programming a bunch of pancakes intermediate sort 1