This morning on a day's class, Chinese back last night a study sister to solve the problem, should not be called to solve, called a point of thinking, because do not know why the program has been repeated output, the afternoon 7, 8 lessons went on the assembly experiment, feeling is and digital circuit circuit nothing to go (and hardware) , in the evening, took 1.5 hours to solve the problem.
The problem is as follows:
A pile of food, the first one to take one, take the remaining One-nineth, the second to take two, and then take the remaining One-nineth
So repeat until the last one takes the rest.
The code is as follows:
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 //a pile of food, the first one to take one, take the remaining One-nineth, the second to take two, and then take the remaining One-nineth7 //so repeat until the last one takes the rest. 8 namespaceSub-food9 {Ten class Program One { A Static voidMain (string[] args) - { - the //int f=10, p;//F for Food quantity, p for number of people - //While (true) - //{ - //int m = f;//temporary storage of food + //While (true) - // { + //p = 1;//every time you start with the first person , A at //Loop: - //if (f-p)% 9 = = 0)//If this heap of food minus p can be divisible by 9, it means that the person in the back can continue to take it . - // { - //if (f-p = = 0)//Judging if the rest of the food is equal to P. - // { - //Console.WriteLine ("Number: {0}, food: {1}", p, m);// in //Console.readkey (); - //return; to // } + //f = P-F-(F-P)/9;//The amount of food left after taking the food - //p++; the //Goto Loop; * // } $ //f = m;//Restore the quantity of the original foodPanax Notoginseng //Break ;//exit number Cycle - // } the //f++;//food increases by 1; + //} A the + //int F, p; - ////bool B = true; $ ////while (b) $ ////{ - //For (f = ten;; ++f) - //{ the //int temp = f; - //p = 1;Wuyi the - Wu //Loop: - //if (f-p)% 9 = = 0) About // { $ //if (f-p = = 0) - // { - //Console.WriteLine ("Number: {0}, food: {1}", p, temp); - //Console.readkey (); A //return; + // } the //f = P-F-(F-P)/9; - //p++; $ //Goto Loop; the // } the //f = temp; the the for(intf=Ten; ; f++) - { inStep (F,1); the } the About // //} the //} the the + } - Public Static voidStepintFintp) the {Bayi the the if(f-P)%9==0) - { - if(F-P =0) the { the for(inti = P-1; i >0; i--) the { thef = f *9/8+i; - } theConsole.WriteLine ("food: {0}, number of people: {1}", F, p); the Console.readkey (); the return;94 } thef = P-F-(F-P)/9; thep++; the step (F, p);98 } About return; - 101 102 103 104 } the }106}
I first use recursion, due to the occurrence of the repeated output of the situation, and later with a for loop, I do not know how to get a code can not access the problem, the last use of the while loop. Although this problem is not difficult, but it took me some time to complete the task, finally I have three ways to achieve .
2015-11-26
Food distribution issues