Link
Today
Total
12-23 15:12
Archives
관리 메뉴

초보개발자 긍.응.성

[Java] 8673. 코딩 토너먼트1 본문

코딩테스트/SW Expert Academy

[Java] 8673. 코딩 토너먼트1

긍.응.성 2019. 10. 28. 21:15
반응형

D3 - 8673. 코딩 토너먼트1

문제출처: https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AW2Jldrqlo4DFASu&categoryId=AW2Jldrqlo4DFASu&categoryType=CODE


2^N개의 숫자를 N번의 round를 돌며 차이를 더해주어 구현한 문제


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {

    public static void main(String\[\] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int T = Integer.parseInt(br.readLine());

        for (int test\_case = 1; test\_case <= T; test\_case++) {
            int N = Integer.parseInt(br.readLine());
            int\[\] power = new int\[(int) Math.pow(2, N)\];
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int i = 0; i < power.length; i++) {
                power\[i\] = Integer.parseInt(st.nextToken());
            }

            int boring = 0;

            for (int i = N; i > 0; i--) {
                for (int j = 0; j < Math.pow(2, i) / 2; j++) {
                    int winner = power\[2 \* j\] > power\[2 \* j + 1\] ? power\[2 \* j\] : power\[2 \* j + 1\];
                    boring += Math.abs(power\[2 \* j\] - power\[2 \* j + 1\]);
                    power\[j\] = winner;
                }
            }

            System.out.println("#" + test\_case + " " + boring);
        }

    }

}
반응형
Comments