백준 자바 10811 1차원배열(바구니뒤집기)

임명수·2023년 5월 12일
0

백준

목록 보기
20/31

https://www.acmicpc.net/problem/10811


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

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

        StringTokenizer st = new StringTokenizer(br.readLine());
        int basket = Integer.parseInt(st.nextToken());
        int count = Integer.parseInt(st.nextToken());

        int[] basketArray = new int[basket];
        for(int i = 0; i < basket; i++){
            basketArray[i] = i+1;
        }

       for(int i = 0; i < count; i++){
            st = new StringTokenizer(br.readLine());
            int from =  Integer.parseInt(st.nextToken()) -1;
            int to =   Integer.parseInt(st.nextToken()) -1;
            while(from < to){
                int temp = basketArray[from];
                basketArray[from] = basketArray[to];
                basketArray[to] = temp;
                from++;
                to--;
            }
        }

        for(int i = 0; i < basket; i++){
            System.out.print(basketArray[i]+ " ");
        }
    }

}
profile
푸른영혼의별

0개의 댓글