简体   繁体   中英

My logical program is not giving the correct output?

Question:

The Utopian tree goes through 2 cycles of growth every year. 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. Now, a new Utopian tree sapling is planted at the onset of the spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

Input Format The first line contains an integer, T, the number of test cases. T lines follow. Each line contains an integer, N, that denotes the number of cycles for that test case.

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

Output Format For each test case, print the height of the Utopian tree after N cycles.

//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...

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....

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; 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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