Java Loops II

Sort by

recency

|

3266 Discussions

|

  • + 0 comments

    Yes, i did the impossible, adding more complexity to solve:

    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 j = 0;
                int q = a+1*b;
                
                while(j < n) 
                {
                    System.out.print(q+" ");
                    q = q + 2*b;
                    b = b*2;
                    j++;
                }
                System.out.println();
            }
            in.close();
        }
    }
    
  • + 0 comments
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int q = scan.nextInt();
        for (int i = 0; i < q; i++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            int n = scan.nextInt();
            for(int j=0; j<n; j++){
                a+=(int)Math.pow(2,j)*b;
                System.out.print(a+" ");
            }
            System.out.println();
        }
        scan.close();
    }
    

    }

  • + 0 comments

    This is my solution. It the is most optimum solution so far.

        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 result = a;
                for (int x = 0; x < n; x++) {
                    // Use (1 << j) instead of Math.pow(2, j) // It's faster and works directly with integers.
                    // result += (b * (int) Math.pow(2, x));
                    result += (b * 1 << x);
                    System.out.printf("%s ",result);
                }
                System.out.println();
            }
            in.close();
        }
    
  • + 0 comments

    This is my solution. It the is most optimum solution so far.

        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 result = a;
                for (int x = 0; x < n; x++) {
                    // Use (1 << j) instead of Math.pow(2, j) // It's faster and works directly with integers.
                    // result += (b * (int) Math.pow(2, x));
                    result += (b * 1 << x);
                    System.out.printf("%s ",result);
                }
                System.out.println();
            }
            in.close();
        }
    
  • + 0 comments
            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 aux=1;
            int sum=a;
            int compteur =0;
            while(compteur<n){
                sum+=aux*b;
                System.out.printf(sum+" ");
                aux*=2;
                compteur++;  
            }
            System.out.println();
            } in.close();
            
        }
    }