Java rewrite "C Classic 100 Questions"--21

Source: Internet
Author: User

"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

Related Article

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.