Java Loops II

Sort by

recency

|

3248 Discussions

|

  • + 0 comments

    import java.util.; import java.io.; class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt();

        for(int i=0;i<t;i++){
            int a = in.nextInt();
            int b = in.nextInt();
            int n = in.nextInt(); 
    
            int multiplier = 0;
    
            for (int j = 0 ; j <  n ; j++){
                multiplier = 2*multiplier+1;
    
                System.out.print(a + b*(multiplier) + " ");
            }
            System.out.print("\n");
        }
    
        in.close();
    }
    

    }

  • + 0 comments

    You don't need Math.pow. It's slower than it needs to be. Can be done with a simple (2*multiplier+1) update per loop:

    import java.util.*;
    import java.io.*;
    
    class Solution{
        public static void main(String []argh){
            Scanner in = new Scanner(System.in);
            int t=in.nextInt(); //number of queries
            
            for(int i=0;i<t;i++){
                int a = in.nextInt();
                int b = in.nextInt();
                int n = in.nextInt(); //number of iterations
                
                int multiplier = 0;
                
                for (int j = 0 ; j <  n ; j++){
                    multiplier = 2*multiplier+1;
                
                    System.out.print(a + b*(multiplier) + " ");
                }
                System.out.print("\n");
            }
                    
            in.close();
        }
    }
    
  • + 1 comment

    is anyone else having hard time understand the question ? i am trying to figure out question by eample

  • + 0 comments

    import java.util.; import java.io.;

    class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); for(int i=0;i

        in.close();
    }
    

    }

  • + 0 comments

    That's my solution

    import java.util.; import java.io.; import java.lang.Math;

    class Solution{ public static void main(String []argh){ Scanner in = new Scanner(System.in); int t=in.nextInt(); double[][] arr = new double[t][]; for(int i=0;i

            arr[i] = new double[n];
            for(int j = 0; j < n; j++){
                arr[i][j] = a+b*(Math.pow(2, j+1) - 1);
            }
        }
    
        for(int i = 0; i < t; i++){
            for(int j = 0; j < arr[i].length; j++){
                System.out.printf("%d ", (int) arr[i][j]);
            }
            System.out.println();
        }
    
        in.close();
    }
    

    }