kt_누적합

source
type 📌 개발노트
topics 600-알고리즘 & 코딩테스트 617 Java
types 문제풀이
import java.io.*;
import java.util.*; 

public class Main {
    public static void main(String[] args) throws IOException { 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine().strip());
        List<String> arr = new ArrayList<>(Arrays.asList(br.readLine().strip().split(" ")));
        
        int padN = 2;
        
        if(n>1){
            while(padN<n){
                padN = padN * 2;
            }
            padN -= n;
            for(int i =0;i<padN;i++){
                arr.add("0");
            }
        }
        // System.out.println(arr);
        
        String output = "";
        do{
            output = String.join(" ",arr) +" \n" + output;
            List<String> narr= new ArrayList<>();
            int tmp = 0;
            for (int i = 0 ; i<arr.size(); i++){
                int a = Integer.parseInt(arr.get(i));
                tmp += a;
                if(i%2==1){
                    narr.add(String.valueOf(tmp));
                    tmp = 0;
                }
            }
            arr = narr;
            // System.out.println(narr);
        }while(arr.size()>1);
        output = String.join(" ",arr) +" \n" + output;
        System.out.println(output); 
    }
}