Java Loops II

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            int q, a, b, n;
            
            Scanner scan = new Scanner(System.in);
            
            q = scan.nextInt();
            
            // read the buffer \n in input stream and ignore
            scan.nextLine();
            
            for (int i = 0; i < q; i++){
                String line = scan.nextLine();
                String[] tokens = line.split(" ");
                
                a = Integer.parseInt(tokens[0]);
                b = Integer.parseInt(tokens[1]);
                n = Integer.parseInt(tokens[2]);
                
                int ex = 0;
                for (int j = 0; j <= n-1; j++){
                    // caching the previous output to avoid repeated computations
                    ex += (int)(Math.pow(2, j) * b);
                    System.out.print(a + ex);
                    System.out.print(" ");
                }
                
                System.out.println();
            }
            scan.close();
        }
    }