[HackerRank]-Java Loops 2
처음 시작할 해커랭크 문제 기초이다.
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
예시 설명 2개의 형식을 입력한다
a, b, n을 입력받는다
a + 2^01 + 2^12…2^1*n까지 구하는 문제입니다. 영문으로 되어있어서 이해하는 능력도 갖춰져야합니다.
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 sum = a;
for(int j=0;j<n;j++){
sum += Math.pow(2,j)*b;
System.out.print(sum+" ");
}
System.out.println();
}
in.close();
}
}
for문을 이해하여 가볍게 풀 수 있다. Math.pow() 함수 이용하면 쉽게 풀 수 있다.