"Program 21"
Title: Monkey Eat Peach problem: The first day of monkeys to take off a number of peaches, immediately ate half, not addicted, and eat one more, the next morning will be the remaining peach eaten half, and eat one more. Every morning after eating the rest of the day half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. For the first day to pick a total number.
1. Procedure analysis: Adopt the method of reverse thinking, infer from the backward forward.
2. Program Source code:
1 /*this article transferred from blog: www.cnblogs.com/java-1002 "program 21"3 title: Monkey Eat Peach problem: The first day of monkeys to take off a number of peaches, immediately ate half, not addicted, and eat one more, the next morning will be the remaining peach eaten half, and eat one more. 4 every morning after eating the rest of the day half of the zero one. When I want to eat again in the morning of the 10th day, I see only one peach left. For the first day to pick a total number. 5 Program Analysis: Adopt the method of reverse thinking, infer from the forward. 6 */7 8 Public classJava21 {9 Ten Public Static voidMain (string[] args) { One intsum = 1; A for(inti = 9; i > 0; i--) { -sum = (sum + 1) * 2; - } theSystem.out.println ("First day total pick up" + sum + "one"); - /*for (int i = 1; i < i++) { - System.out.print ("first" + i + "days before eating" + sum + "a"); - int temp = SUM; + int eat = SUM/2 + 1; - sum = sum/2-1; + System.out.println (", eat" + Eat + "one, remainder" + sum + "one"); A }*/ at } -}
Java rewrite "C Classic 100 Questions"--21