Utopian Tree in Java
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的更多相关文章
- 【HackerRank】Utopian tree
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occu ...
- Balanced Binary Tree(Java代码没有结束,是什么原因???)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)
这是悦乐书的第285次更新,第302篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671).给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树 ...
- LeetCode算法题-Trim a Binary Search Tree(Java实现)
这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...
- LeetCode算法题-Average of Levels in Binary Tree(Java实现)
这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...
- LeetCode算法题-Construct String from Binary Tree(Java实现)
这是悦乐书的第273次更新,第288篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第141题(顺位题号是606).构造一个字符串,该字符串由二叉树中的括号和整数组成,并具 ...
- LeetCode算法题-Subtree of Another Tree(Java实现)
这是悦乐书的第265次更新,第278篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第132题(顺位题号是572).给定两个非空的二进制树s和t,检查树t是否具有完全相同的 ...
- LeetCode算法题-Symmetric Tree(Java实现)
这是悦乐书的第163次更新,第165篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第22题(顺位题号是101).给定二叉树,检查它是否是自身的镜像(即,围绕其中心对称). ...
- PART(Persistent Adaptive Radix Tree)的Java实现源码剖析
论文地址 Adaptive Radix Tree: https://db.in.tum.de/~leis/papers/ART.pdf Persistent Adaptive Radix Tree: ...
随机推荐
- 用sql从一张表更新数据到另外一张表(多表数据迁移)
update TBL_1 A, TBL_2 B, TBL_3 Cset a.email=c.email_addrwhere a.user_id=b.user_id and b.un_id=c.un_i ...
- centos上手动编译安装tmux的问题
https://blog.linuxeye.com/323.html 装个tmux也是不容易.. 关键词:libevent要自己下载2.0版,然后编译.安装时还要各种参数指定目录.
- 按enter执行click或者搜索问题
最近需要将按按钮搜索的功能,改为支持按enter回车键,搜索. 实现按enter,执行click事件还是比较简单的. //按回车搜索用户 $(window).keydown(function (eve ...
- AT91-PWM应用
步骤1: make menuconfig配置内核, 开启PWM输出功能. Device Drivers ---> Misc devices ---> <*>Atmel AT3 ...
- Exception-异常
异常(Exception)是程序执行过程中所产生的问题 产生原因:用户输入无效数字.找不到需要打开的文件.在通讯中网络连接中断.JVM发生了内存溢出 异常的三个种类:检查异常.运行时异常.错误(类似异 ...
- php openssl 增加密钥
生成私钥:openssl genrsa 1024 > private.key (注意,1024是密钥的长度,如果密钥较长,相应加密后的密文也会较长) 生成公钥:openssl rsa -in ...
- 图层的transform属性
Main.storyboard // // ViewController.m // 7A11.图层的transform属性 // // Created by huan on 16/2/4. // ...
- python基础整理笔记(三)
一. python的几种入参形式:1.普通参数: 普通参数就是最一般的参数传递形式.函数定义处会定义需要的形参,然后函数调用处,需要与形参一一对应地传入实参. 示例: def f(a, b): pri ...
- centos安装 mysql
centos安装 mysql 1. 下载mysqlmysql被oracle收购后现在退出了企业版和社区版本,社区版本是开源的,企业版是收费的.社区版可以下载源码也可以下载二进制文件包.源码安装比较麻烦 ...
- linux系统基础网络配置
1.修改主机名 临时方法: 退出当前shell重新登录即可生效.此法只能临时修改生效.重启系统后失效. 提示:很多人使用hostname主机名来修改,其实这个只是作为暂时的.重启后将恢复到配置前的主机 ...