繁体   English   中英

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

[英]My logical program is not giving the correct output?

题:

乌托邦树每年要经历 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  :(

错误- 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 * 高度)出现在赔率索引上,第二个周期(1 + 高度)出现在偶数索引上,从 1 到 n(含),起始索引 0 始终为 1。

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

这是我的第一个贡献,只学习编码和解决算法,我必须找到一个可行的解决方案,并且可以简单地遵循代码信用到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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM