Utopian Tree in Java

Source: Internet
Author: User
Tags rounds

The utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the monsoon, when it doubles in height. The second growth cycle occurs during the summer, when its height is increases by 1 meter.
Now, a new utopian tree sapling are planted at the onset of the monsoon. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

Input Format
The first line contains an integer, T, the number of test cases.
T lines follow. Each line contains a integer, N, that denotes the number of cycles for the.

Constraints
1 <= T <= 10
0 <= N <= 60

Output Format
For each test case, print the height of the utopian tree after N cycles.

Sample Input #00:

201

Sample Output #00:

12

explanation #00:
There is 2 test cases. When N = 0, the height of the tree remains unchanged. When N = 1, the tree doubles its height as it's planted just before the onset of monsoon.

Sample Input: #01:

234

Sample Output: #01:

67

Explanation: #01:
There is 2 testcases.
N = 3:
The height of the tree at the end of the 1st cycle = 2
The height of the tree at the end of the 2nd cycle = 3
The height of the tree at the end of the 3rd cycle = 6

N = 4:
The height of the tree at the end of the 4th cycle = 7

As above, Utopia Tree, the first line of input is there are several test case, followed by each test case in the tree to long several rounds

Tree if the long odd wheel is every time on the original basis +1, even the number of times the number of rounds is the original base, if not growth (n=0), then the initial height of the tree is 0.

So the puzzle is simply to make the odd and even judgment accumulation.

Mainly use this question to review how to use scanner in Java

The code is as follows:

1Importjava.io.*;
2 Importjava.util.*;
3Importjava.text.*;
4Importjava.math.*;
5Importjava.util.regex.*;
6
7 PublicclassSolution {
8
9
TenStaticintHelperintNUM) {
Oneinti = 1;
Aintbase = 1;
- while(I<=num) {
-if(i%2==0) {
thebase = base+1;
-}Else{
-base = base*2;
-}
+i++;
-}
+returnBase
A}
at
-
- PublicStaticvoidMain (string[] args) {
-Scanner in =NewScanner (system.in);
-intT
-t = In.nextint ();
inint[] n =Newint[T];
-
tointi = 0;
+ while(In.hasnext () &&i<t) {
-N[i] = In.nextint ();
thei++;
*}
$
Panax Notoginsengint[] res =Newint[T];
- for(i = 0; i < T; i++)
theRes[i] = helper (n[i]);
+
A for(i = 0; i<res.length; i++)
theSystem.out.println (Res[i]);
+
-
$}
$}

Utopian Tree in Java

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.