簡體   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