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

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

WBOY
WBOYforward
2023-04-21 08:55:151125browse

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete