【HackerRank】Utopian tree

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 after N growth 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 after N cycles.


题解:

 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 }

 

转载于:https://www.cnblogs.com/sunshineatnoon/p/3874293.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值