728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com

queue 사용
import java.util.ArrayDeque;
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int tc=1;tc<=t;tc++) {
int k = sc.nextInt();
int sum=0;
ArrayDeque<Integer> q = new ArrayDeque<>();
for(int i=0;i<Math.pow(2, k);i++) {
q.add(sc.nextInt());
}
while(q.size()>=2) {
int a = q.poll();
int b = q.poll();
sum+=Math.abs(a-b);
q.add(Math.max(a, b));
}
System.out.println("#"+tc+" "+sum);
}
}
}
array (배열) 사용
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int tc = 1; tc <= t; tc++) {
int k = (int) Math.pow(2, sc.nextInt());
int arr[] = new int[k];
for (int i = 0; i < k; i++) {
arr[i] = sc.nextInt();
}
int sum = 0;
while (k > 0) {
for (int i = 0; i < k / 2; i++) {
int a = arr[i * 2];
int b = arr[i * 2 + 1];
sum += Math.abs(a - b);
arr[i] = Math.max(a, b);
}
k /= 2;
}
System.out.println("#" + tc + " " + sum);
}
}
}728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 동철이의 프로그래밍 대회 (D3) (0) | 2024.08.31 |
|---|---|
| [SW Expert Academy] 준홍이의 카드놀이 (D3) (0) | 2024.08.31 |
| [SW Expert Academy] [S/W 문제해결 기본] 3일차 - 회문1 (0) | 2024.08.29 |
| [SW Expert Academy] 다양성 측정 (D3) (0) | 2024.08.29 |
| [SW Expert Academy] 민석이의 과제 체크하기 (D3) (0) | 2024.08.29 |