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