매일 Algorithm

신재원·2023년 2월 7일
0

Algorithm

목록 보기
30/243

백준 2947번 (bronze 1)

import java.util.Scanner;

public class problem42 {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int[] array = new int[5];

        for (int i = 0; i < array.length; i++) {
            array[i] = in.nextInt();
        }
        /*
        이중 for문으로 한 이유는
        예시로 2 3 4 5 1이 입력되었을 경우 5 1 하고만 비교되고 끝나게 됨으로
        이중 array.length 사이즈만큼 이중 for문을 하였다.
        */
        for (int j = 0; j < array.length; j++) {
            for (int i = 0; i < array.length - 1; i++) {
                if (array[i] > array[i + 1]) {
                    int temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                    for (int solution : array) {
                        System.out.print(solution + " ");
                    }
                    System.out.println();
                }
            }
        }
    }
}

0개의 댓글