[英]My logical program is not giving the correct output?
Question:题:
The Utopian tree goes through 2 cycles of growth every year.乌托邦树每年要经历 2 个生长周期。 The first growth cycle occurs during the spring, when it doubles in height.
第一个生长周期发生在春季,此时高度翻倍。 The second growth cycle occurs during the summer, when its height increases by 1 meter.
第二个生长周期发生在夏季,此时其高度增加 1 米。 Now, a new Utopian tree sapling is planted at the onset of the spring.
现在,一棵新的乌托邦树苗在春天开始时种植。 Its height is 1 meter.
它的高度是1米。 Can you find the height of the tree after N growth cycles?
你能找到 N 个生长周期后树的高度吗?
Input Format The first line contains an integer, T, the number of test cases.输入格式 第一行包含一个整数 T,即测试用例的数量。 T lines follow.
T线紧随其后。 Each line contains an integer, N, that denotes the number of cycles for that test case.
每行包含一个整数 N,表示该测试用例的循环数。
Constraints
1 <= T <= 10
0 <= N <= 60
Output Format For each test case, print the height of the Utopian tree after N cycles.输出格式对于每个测试用例,在 N 个循环后打印乌托邦树的高度。
//FINALLY, HOPE so .. WHAT QUESTION IS SAYING.. //最后,希望如此.. 问题在说什么..
INITIALLY VALUE IS 1 .. IF SPRING OCCURS.. IT'S VALUE WILL BE DOUBLED.. THAT MEANS .. IT WILL BE MULTIPLIED BY 2.. BUT IF SUMMER OCCUR IT'S VALUE WILL BE ADDED BY 1...最初的价值是 1 .. 如果春天发生.. 它的价值将翻倍.. 这意味着.. 它会乘以 2.. 但如果夏天发生它的价值将增加 1 ...
If i give input:如果我提供输入:
2 //here 2 is the number of question..
0
1
So, Output must be:所以,输出必须是:
1
2
Another example,另一个例子,
sample of output:输出样本:
2
3
4
So, Sample of input will be:因此,输入样本将是:
6
7
HOPE SO.. YOU UNDERSTAND WHAT QUESTION IS ASKING, HERE NOW WE HAVE TO MAKE A PROGRAM INTO JAVA....希望如此......你明白问题在问什么,现在我们必须将程序变成Java......
Okay as further i made a program for this..好的,我为此做了一个程序。
package com.logical03;
import java.util.Scanner;
public class MainProgram{
public static void main(String[] args){
int num=1;
int[] array=new int[100];
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of Questions: ");
int n_Elements=in.nextInt();
System.out.println("Enter the values now: ");
for(int i=1; i<=n_Elements; i++){
array[i]=in.nextInt();
}
for(int i=1; i<=n_Elements; i++){
if(array[i]==0){
System.out.println("\n1");
}
else{
for(int j=1; j<=array[i]; j++){
if(j%2!=0){
num=num*2;
}
else{
num=num+1;
}
}
System.out.println(num);
}
}
}
}
As i run into here .. it adds the second number of question into my output.. Suppose..当我遇到这里时..它在我的输出中添加了第二个问题..假设..
If i give input as:如果我输入如下:
2
3
4
So, output must suppose to be:因此,输出必须假设为:
6
7
Which is correct!!哪个是正确的!!
But My program gives the output as:但我的程序给出的输出为:
6
27 //which is incorrect..becoz it adds the sum of above number :(
Mistake - int num = 1;
错误-
int num = 1;
should be declared in inside parent loop to refresh it's value.应该在父循环内部声明以刷新它的值。
public static void main(String[] args) {
int[] array = new int[100];
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of Questions: ");
int n_Elements = in.nextInt();
System.out.println("Enter the values now: ");
for (int i = 1 ; i <= n_Elements ; i++) {
array[i] = in.nextInt();
}
for (int i = 1 ; i <= n_Elements ; i++) {
int num = 1;
if (array[i] == 0) {
System.out.println("\n1");
} else {
for (int j = 1 ; j <= array[i] ; j++) {
if (j % 2 != 0) {
num = num * 2;
} else {
num = num + 1;
}
}
System.out.println(num);
}
}
}
Output输出
Enter the number of Questions:
2
Enter the values now:
3
4
6
7
My approach is to take on account that first cycle (2 * height) occurs on odds indexes, and second cicle (1 + height) occurs on even indexes, from 1 to n (inclusive), starting index 0 is always 1.我的方法是考虑到第一个周期(2 * 高度)出现在赔率索引上,第二个周期(1 + 高度)出现在偶数索引上,从 1 到 n(含),起始索引 0 始终为 1。
return IntStream.rangeClosed(1, n)
.reduce(1, (acc, idx) -> idx % 2 != 0 ? acc * 2 : acc + 1);
This is my first contribution, only learning to code and solve algorithms, I had to find a workable solution with simple to follow code credit to http://www.javainterview.net/HackerRank/utopian-tree这是我的第一个贡献,只学习编码和解决算法,我必须找到一个可行的解决方案,并且可以简单地遵循代码信用到http://www.javainterview.net/HackerRank/utopian-tree
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
//receive input
Scanner in = new Scanner(System.in);
//no of test cases
int T=in.nextInt();
//no of cycles
int[] N = new int[T];
for(int i=0;i<T;i++){
N[i]=in.nextInt();
}
int height=1;
for(int i=0;i<N.length;i++){
height=1;
for(int j=1;j<=N[i];j++){
if((j%2) ==1)
height=height*2;
else
height++;
}
System.out.println(height);
}
}
}//this the end of the class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.