【问题标题】:My logical program is not giving the correct output?我的逻辑程序没有给出正确的输出?
【发布时间】:2014-10-22 05:56:54
【问题描述】:

问题:

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

输入格式 第一行包含一个整数 T,即测试用例的数量。 T 线紧随其后。每行包含一个整数 N,表示该测试用例的循环数。

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

输出格式 对于每个测试用例,打印 N 个循环后乌托邦树的高度。

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

最初的价值是 1 .. 如果春天发生.. 它的价值将加倍.. 这意味着.. 它将乘以 2.. 但如果夏天发生,它的价值将增加 1...

如果我提供意见:

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

所以,输出必须是:

1
2

另一个例子,

输出样本:

2
3
4

因此,输入样本将是:

6
7

希望如此.. 你明白问题在问什么,现在我们必须将程序编写成 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  :(

【问题讨论】:

  • 对不起,我不是天才。我只是人类
  • 我不明白您的输入和输出示例。输入“2,3,4”如何加起来为“27”?您能否澄清或清理帖子。你看@KRUKUSA 也不明白...
  • 答案发布给大家接受/赞成
  • @Rajan,请说出问题数量是什么意思?此外,如果您在提出更多问题之前先阅读社区的指南,那也很好。
  • @AlvinBunk:再读一遍这个问题……ankur 解决了……有一个愚蠢的错误……

标签: java spring


【解决方案1】:

错误 - 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

【讨论】:

    【解决方案2】:

    我的方法是考虑到第一个循环(2 * 高度)发生在赔率索引上,第二个循环(1 + 高度)发生在偶数索引上,从 1 到 n(含),起始索引 0 始终为 1 .

    return IntStream.rangeClosed(1, n)
                .reduce(1, (acc, idx) -> idx % 2 != 0 ? acc * 2 : acc + 1);
    

    【讨论】:

      【解决方案3】:

      这是我的第一个贡献,只学习编码和解决算法,我必须找到一个可行的解决方案,简单易懂的代码归功于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
      

      【讨论】:

      • 欢迎来到 StackOverflow.com!在代码中添加解释会很有帮助,这样读者就可以轻松理解问题所在,以及这段代码如何解决问题。
      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 2023-03-30
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      相关资源
      最近更新 更多