Algorithm--The monkey divided the Peach

Source: Internet
Author: User

Want to do some of the "algorithm" exercises, in the online inadvertently saw a very classic algorithm problem-monkey points peach, so Baidu a bit, only to find that the title of the relevant documents a lot of many, but the most is the use of C + + or Java to solve the problem, but also the first contact C + +.

1. First introduce the most common editor--dev CPP used by C + +. Attached: (http://bloodshed-dev-c.en.softonic.com/download);

2.;

3. Then choose the address that you want to download well;

4. The remaining thing is "default" until the installation is complete.

Then just start dev C + +, after starting dev C + +, create a new project: You can press CTRL + N or directly point "file"-"new"-"source code", and then you need to write the code.

After clicking Run, select "Run"--"compile"--"Run" or simply click "Run"-"compile run" to run the project.

There is also a way to create a new project:

"File"--"new"-"project", then press Select Console Application, then write the file name. Run the same way as above. Just for the first time when running C + + code, actually appeared a "splash screen" phenomenon, and finally in return 0 before adding a "System (" PAUSE "); "To make the program run correctly.

Attach the first algorithm problem of C + +:

  

    /* * There's a bunch of peaches on the beach, five monkeys. The first monkey divided this pile of peaches into five copies, one more, and the monkey threw one more into the sea and took a copy. The second monkey to the rest of the peach evenly divided into five, and more * one, it also threw a lot of one into the sea, took a copy, the third, the fifth monkey is doing this, asked the beach at least how many peaches? **/

Run C + + code:

#include <stdio.h>#include<stdlib.h>intMonkey_peach (intN) {int*p; inti; P= (int*)malloc(n *sizeof(int)); if(p = =NULL)return-1; p[0] =1;  while(1)     {          for(i =1; I < n; i++)         {             if((p[i-1] *5+1) %4!=0)                  Break; P[i]= (p[i-1] *5+1) /4; }          if(I >=N) Break; p[0]++; }       for(i =0; I < n; i++) {printf ("%d", P[i]); } printf ("\ n"); printf ("Peach count =%d.\n",5* p[n-1] +1); return 0; } intMain () {Monkey_peach (5); System ("PAUSE"); return 0; }

Just now look at this code of C + +, I still lack understanding, and then research and research ...

Operation Result:

Suddenly also think C + + is very magical, willing to learn more after their own!

Algorithm--The monkey divided the Peach

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.