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 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.

Sample Input #00:

2
0
1

Sample Output #00:

1
2

Explanation #00:
There are 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:

2
3
4

Sample Output: #01:

6
7

Explanation: #01:
There are 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

题目如上,乌托邦树,第一行输入是有几个test case,后面是每一个test case中树要长几轮

树如果长奇数轮就是每次在原基础上+1,偶数次轮数就是每次原基础*2,如果没有增长(n=0),那么树的初始高度为0.

所以题解就是简单的进行奇偶判断累加即可。

主要利用这道题复习java中如何使用scanner

代码如下:

 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 
     static int helper(int num) {
         int i = 1;
         int base = 1;
         while(i<=num){
             if(i%2==0){
                 base = base+1;
             }else{
                 base = base*2;
             }
             i++;
         }
         return base;
    }
 
    
  public static void main(String[] args) {
         Scanner in = new Scanner(System.in);
         int t;
         t = in.nextInt();
         int [] n = new int[t];
      
         int i = 0;
         while(in.hasNext()&&i<t){
             n[i] = in.nextInt();
             i++;
         }
      
        int [] res = new int[t];
        for(i = 0; i < t; i++)
            res[i] = helper(n[i]);
        
        for(i = 0; i<res.length; i++)
            System.out.println(res[i]);
        
        
    }
 }

Utopian Tree in Java的更多相关文章

  1. 【HackerRank】Utopian tree

    The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...

  2. Balanced Binary Tree(Java代码没有结束,是什么原因???)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  3. LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)

    这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...

  4. LeetCode算法题-Trim a Binary Search Tree(Java实现)

    这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...

  5. LeetCode算法题-Average of Levels in Binary Tree(Java实现)

    这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...

  6. LeetCode算法题-Construct String from Binary Tree(Java实现)

    这是悦乐书的第273次更新,第288篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第141题(顺位题号是606).构造一个字符串,该字符串由二叉树中的括号和整数组成,并具 ...

  7. LeetCode算法题-Subtree of Another Tree(Java实现)

    这是悦乐书的第265次更新,第278篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第132题(顺位题号是572).给定两个非空的二进制树s和t,检查树t是否具有完全相同的 ...

  8. LeetCode算法题-Symmetric Tree(Java实现)

    这是悦乐书的第163次更新,第165篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第22题(顺位题号是101).给定二叉树,检查它是否是自身的镜像(即,围绕其中心对称). ...

  9. PART(Persistent Adaptive Radix Tree)的Java实现源码剖析

    论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...

随机推荐

  1. ImportError: No module named setuptools 解决方案

    shell中输入: wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz --no-che ...

  2. su - root 报su: incorrect password的错误

    检查/bin/下面的文件的组属 和 sh 文件 的权限 是否有问题 例如:-rwxr-xr-x. 1 weblogic dba 34904 Jul 15  2011 /bin/su1   修改这个文件 ...

  3. rocksdb编译测试的正确姿势

    需要先安装 gflags 在进行 make db_bench 不然运行 db_bench 会出现 Please install gflags to run rocksdb tools 错误 bench ...

  4. SDAutoLayout:比masonry更简单易用的自动布局库

    SDAutoLayout:一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库. [SDAutoLa ...

  5. Java 关键字、标识符、注释、常量与变量、数据类型,算术、赋值、比较、逻辑、位、三元运算符和流程控制、break、continue【3】

    若有不正之处,请多多谅解并欢迎批评指正,不甚感激.请尊重作者劳动成果: 本文原创作者:pipi-changing本文原创出处:http://www.cnblogs.com/pipi-changing/ ...

  6. AngularJs的$http使用随笔

    AngularJs的$http服务是Angularjs自带的核心服务之一,用来与HTTP远程服务器交互. 关于$http使用,我体会的一下几点注意: 1.在使用是报“Uncaught Referenc ...

  7. Android性能优化方法(七)

    Java从JDK1.2版本开始,就把对象的引用分为四种级别,从而使程序能更加灵活的控制对象的生命周期.这四种级别由高到低依次为:强引用.软引用.弱引用和虚引用. 这里重点介绍一下软引用和弱引用. 如果 ...

  8. Python动态生成变量

    Python 也可以像javascript那样动态生成变量.我们看javascript的动态生成变量. var obj = {}; for (var i =0, len = 10; i < le ...

  9. Swing Note

                                      2. Swing容器:   内容窗格.分层窗格.玻璃窗格和一个可选的菜单条.(这四个同时包含在根窗格里)(请分别向其中添加组件)   ...

  10. Linux 配置主机名

    方法 1:临时配置 [root@NING ~]# hostname NING CRT重新连接即可,服务器重启失效. 方法 2:永久配置 步骤1:包含了主机最基本的网络信息,用于系统启动. [root@ ...