본문 바로가기

[백준] 2947번: 나무 조각 (자바)

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


<코드>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
//버블정렬
public class Main {
    public static void main(String args[]) throws IOException {
        int[] piece = new int[5];
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] nums;
 
        nums = br.readLine().split(" ");
        for (int i = 0; i < piece.length; i++)
            piece[i] = Integer.parseInt(nums[i]);
 
        int tmp=0;
        for(int i=0; i<piece.length-1;i++){
            for (int j=0; j<piece.length-1; j++){
                if(piece[j]>piece[j+1]){
                    tmp=piece[j];
                    piece[j]=piece[j+1];
                    piece[j+1]=tmp;
                    for(int p :piece)
                        System.out.print(p+" ");
                    System.out.println();
                }
            } // for
        } // for
    } // main
}
cs


정렬만 사용할 줄 안다면 간단하게 풀 수 있는 문제였다.