[Hackerrank] utopian tree

Source: Internet
Author: User

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

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

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

Output Format
For each test case, print the height of the utopian tree afterNCycles.

Question:

 1 import java.io.*; 2 import java.util.*; 3 import java.text.*; 4 import java.math.*; 5 import java.util.regex.*; 6  7 public class Solution { 8  9 10     static int Utopian_Tree(int N) {11         if(N == 0)12             return 1;13         if(N == 1)14             return 2;15         if(N%2 == 0)16             return (int)Math.pow(2, N/2+1)-1;17         return (int)Math.pow(2, N/2+2)-2;18    }19 20    21  public static void main(String[] args) {22         Scanner in = new Scanner(System.in);23         int t;24         t = in.nextInt();25         for(int i = 0;i < t;i++){26             int n;27             n = in.nextInt();28             System.out.println(Utopian_Tree(n));29         }30    }31 }

 

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.