Home > Java > javaTutorial > Use Java to write a solution to the monkey eating peach problem

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-21 08:55:15
forward
1258 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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template