One of the beauty of programming stacks of pancakes sort 1

Source: Internet
Author: User

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 (&AMP;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

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.