HackerRank/HR-Java

#6 Java Loops

후뤼한잉여 2015. 11. 18. 00:38

https://www.hackerrank.com/challenges/java-loops


Problem Statement

In this problem you will test your knowledge of Java loops. Given three integers ab, and n, output the following series:

a+20b,a+20b+21b,......,a+20b+21b+...+2n1b

Input Format

The first line will contain the number of testcases t. Each of the next t lines will have three integers, ab, and n.

Constraints:

0a,b50
1n15

0a,b50
1n15

Output Format

Print the answer to each test case in separate lines.

Sample Input

2
0 2 10
5 3 5

Sample Output

2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98

Explanation

In the first case:

1st term=0+1*2=2
2nd term=0+1*2+2*2=6
3rd term=0+1*2+2*2+4*2=14

and so on.

보낸 답)

테스트 케이스가 많으면 메모리를 많이 먹겠지만 초보니까 쿨하게 패스~!(+ 거기다 범위가 있었다는걸 나중에 깨달음...)

import java.util.*;

import java.math.*;


public class Solution {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        

        int testCaseCnt = sc.nextInt();

        int a[] = new int[testCaseCnt];

        int b[] = new int[testCaseCnt];

        int n[] = new int[testCaseCnt];

        int sum;

        

        for(int i=0; i<testCaseCnt; i++) {

            a[i] = sc.nextInt();

            b[i] = sc.nextInt();

            n[i] = sc.nextInt();

        }

        

        for(int i=0; i<testCaseCnt; i++) {

            sum = 0;

            

            for(int j=0; j<n[i]; j++) {

                if(j == 0) {

                    sum +=  a[i];

                }

                sum += (b[i] * (int)Math.pow(2,j));

                System.out.print(sum + " ");

            }

            System.out.println();

        }

    }

}