我的逻辑程序没有给出正确的输出?

问题描述:

问:我的逻辑程序没有给出正确的输出?

乌托邦树经过每年2个周期的增长。第一个生长周期发生在春季,当时它的高度加倍。第二个生长周期发生在夏季,其高度增加1米。 现在,一个新的乌托邦树苗在春季开始播种。它的高度是1米。你能在N个生长周期后找到树的高度吗?

输入格式 第一行包含一个整数T,即测试用例的数量。 T行跟随。每行包含一个整数N,表示该测试用例的周期数。

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

输出格式 对于每一个测试的情况下,打印乌托邦树的高度N个周期之后。

//最后,希望如此。什么问题是说..

INITIALLY值为1 .. IF SPRING OCCURS .. IT的值将加倍..这意味着..它将被乘以2 ..但如果夏天OCCUR IT的价值将被添加BY 1 ...

如果我给输入:

2  //here 2 is the number of question.. 
0 
1 

所以,输出必须是:

1 
2 

又如,

样本输出的:

2 
3 
4 

所以,输入的样本将是:

6 
7 

HOPE SO ..你了解问题是问,该处现在我们已经使程序进入JAVA ....

好吧,随着进一步我做了这个程序..

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); 
       } 
      } 
     } 
    } 

正如我遇到了在这里..它增加了问题的第二号到我的输出..假设..

如果我给输入为:

2 
3 
4 

因此,输出必须猜想是:

6 
7 

这是正确的!

但我的程序给出了输出为:

6 
27 //which is incorrect..becoz it adds the sum of above number :(
+0

对不起,我不是天才。我只是人 – KRUKUSA 2014-10-22 05:58:14

+0

我不明白你的输入和输出的例子。输入'2,3,4'如何加起来为'27'?你能澄清或清理这个帖子吗?你看到@KRUKUSA也不明白... – 2014-10-22 06:06:01

+0

答案发布给大家接受/ upvote – 2014-10-22 06:10:28

错误 - int num = 1;应该在里面父循环声明刷新它的价值。

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); 
      } 
     } 
    } 

输出

Enter the number of Questions: 
2 
Enter the values now: 
3 
4 
6 
7 
+0

是的,有一个愚蠢的错误..:p – 2014-10-22 06:13:30

这是我的第一笔捐款,只有不断学习才能代码和解决的算法,我必须找到简单可行的解决方案遵循代码信贷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 
+0

欢迎来到*.com!在代码中添加说明会很有帮助,因此读者可以轻松理解问题所在,以及代码如何修复问题。 – 2015-05-23 13:44:56