Home > Java > javaTutorial > body text

Use Java to write a solution to the monkey eating peach problem

WBOY
Release: 2023-04-21 08:55:15
forward
1054 people have browsed it

Title description:

Wukong picked a few peaches on the first day and ate half of them immediately. When he was not satisfied, he ate one more. On the second day, he ate half and one more of the remaining peaches. From then on, he ate one more than half of the remaining peaches from the previous day every day. , and when I was ready to eat on the nth day, there was only one peach left. If you are smart, please help Wukong calculate how many peaches there were when he started eating on the first day?

Recommended related video tutorials: java online tutorial

Input:

Enter a number n (1<=n<=30).

Output:

Output the number of peaches on the first day.

Sample input:

​3

Sample output:

10

code:

​importjava.util.*;

publicclassMain

​{

​publicstaticvoidmain(String[]args)

​{

Scannerinput=newScanner(System.in);

​intn=input.nextInt();

inta=1;

​for(inti=1;i

​{

a=(a 1)*2;

}

System.out.println(a);

}

}

The above is the detailed content of Use Java to write a solution to the monkey eating peach problem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!