Java Loops II

Sort by

recency

|

3258 Discussions

|

  • + 0 comments

    For Java15 Platform

    I wrote the code from scratch just to get more practice

    import java.util.Scanner;
    
    class Solution
    {
        public static void main(String args[])
        {
            Scanner sc = new Scanner(System.in);
            int q = sc.nextInt();
            
            for(int i=0; i<q; i++)
            {
                int a, b, n;
                a = sc.nextInt();
                b = sc.nextInt();
                n = sc.nextInt();
                
                int series = a;
                
                for(int j=0; j<n; j++)
                {
                    series += Math.pow(2, j) * b;
                    
                    System.out.print((int)series + " ");
                }
                
                System.out.print("\n");
            }
            
            sc.close();
        }
    }
    
  • + 0 comments

    Java

    Solution using Map for memoization for better performance

    public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            sc.nextLine();
            while(sc.hasNextLine()){
                String[] linea = sc.nextLine().split(" ");
                int a = Integer.parseInt(linea[0]); 
                int b = Integer.parseInt(linea[1]);
                int n = Integer.parseInt(linea[2]);
                
                // Create a map in order to save previously calculated values
                Map<Integer, Double> map = new HashMap<>();
                StringBuilder sb = new StringBuilder();
                
                map.put(0, Math.pow(2, 0)*b);
                for(int i = 1; i < n; i++){
                    map.put(i, map.get(i-1) + Math.pow(2, i)*b);
                }
                map.values().forEach(v -> sb.append( a + Math.round(v) +" "));
                System.out.println(sb.substring(0, sb.length()-1))    ;
                
            }
            sc.close();
        }
    
  • + 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 result = a + (int) Math.pow(2,0) * b; 
                System.out.print(result + " ");
                for(int j = 1; j < n; j++){
                    result += (int) Math.pow(2,j) * b;
                    System.out.print(result + " ");
                }
                System.out.println();
                
            }
            in.close();
        }
    }
    
  • + 0 comments

    Mannn... why is it hard for me ;-;

    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++){
                Double sum;
                int a = in.nextInt();
                int b = in.nextInt();
                int n = in.nextInt();
                
                sum = a + (Math.pow(2, 0) * b);
                
                    for(int j=1; j<=n; j++){ System.out.printf("%.0f ", sum); 
                    sum += (Math.pow(2, j) * b);
                    }
                System.out.println();
                
                // for(){
                //     ;
                // }
                
            }
            in.close();
        }
    }
    
  • + 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++){
                double sum = 0;
                int a = in.nextInt();
                int b = in.nextInt();
                int n = in.nextInt();
                sum = a + (Math.pow(2, 0) * b);
                    for (int j = 1; j <= n; j++){
                        System.out.printf("%.0f ", sum );
                        sum += (Math.pow(2, j)) * b;
                    }
                System.out.println();
            }
            in.close();
        }
    }