https://www.hackerrank.com/challenges/java-loops
Problem Statement
In this problem you will test your knowledge of Java loops. Given three integers
Input Format
The first line will contain the number of testcases t. Each of the next
Constraints:
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();
}
}
}
'HackerRank > HR-Java' 카테고리의 다른 글
#9 Java Static Initializer Block (0) | 2015.11.23 |
---|---|
#8 Java End-of-file (0) | 2015.11.23 |
#5 Java Output Formatting (0) | 2015.11.17 |
#4 Java Stdin and Stdout 2 (0) | 2015.11.17 |
#3 Java If-Else (0) | 2015.11.17 |