๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

[๋ฐฑ์ค€/Java] 2562 ์ตœ๋Œ“๊ฐ’, 10810 ๊ณต ๋„ฃ๊ธฐ, 10813 ๊ณต ๋ฐ”๊พธ๊ธฐ (๋ฐฑ์ค€ 4๋‹จ๊ณ„ 1์ฐจ์›๋ฐฐ์—ด ๋ชจ์Œ)

by ๋ฝ€์งœ๊ผฌ 2025. 1. 20.
728x90
๋ฐ˜์‘ํ˜•

๋ฐฑ์ค€

 

2562 ์ตœ๋Œ“๊ฐ’


๋ฌธ์ œ

9๊ฐœ์˜ ์„œ๋กœ ๋‹ค๋ฅธ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ์ด๋“ค ์ค‘ ์ตœ๋Œ“๊ฐ’์„ ์ฐพ๊ณ  ๊ทธ ์ตœ๋Œ“๊ฐ’์ด ๋ช‡ ๋ฒˆ์งธ ์ˆ˜์ธ์ง€๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์„œ๋กœ ๋‹ค๋ฅธ 9๊ฐœ์˜ ์ž์—ฐ์ˆ˜

3, 29, 38, 12, 57, 74, 40, 85, 61

์ด ์ฃผ์–ด์ง€๋ฉด, ์ด๋“ค ์ค‘ ์ตœ๋Œ“๊ฐ’์€ 85์ด๊ณ , ์ด ๊ฐ’์€ 8๋ฒˆ์งธ ์ˆ˜์ด๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ ์•„ํ™‰ ๋ฒˆ์งธ ์ค„๊นŒ์ง€ ํ•œ ์ค„์— ํ•˜๋‚˜์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ž์—ฐ์ˆ˜๋Š” 100 ๋ณด๋‹ค ์ž‘๋‹ค.

3
29
38
12
57
74
40
85
61

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ณ , ๋‘˜์งธ ์ค„์— ์ตœ๋Œ“๊ฐ’์ด ๋ช‡ ๋ฒˆ์งธ ์ˆ˜์ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

85
8

์ตœ๋Œ“๊ฐ’์ด ๋ช‡๋ฒˆ์งธ ์ค„์˜ ์ˆ˜์ธ์ง€ ๋ฌผ์–ด๋ณด๋Š” ๋ฌธ์ œ์ด๋‹ค.

import java.io.*;

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

        int[] arr = new int[9];
        int max = Integer.MIN_VALUE;
        int index = 0;

        for (int i = 0; i < 9; i++) {
            arr[i] = Integer.parseInt(br.readLine());
            if (arr[i] > max) {
                max = arr[i];
                index = i+1;
            }
        }

        System.out.println(max);
        System.out.println(index);
    }
}

10810 ๊ณต ๋„ฃ๊ธฐ


๋ฌธ์ œ

๋„ํ˜„์ด๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ด N๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค. ๋˜, 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์ ํ˜€์žˆ๋Š” ๊ณต์„ ๋งค์šฐ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ฐ€์žฅ ์ฒ˜์Œ ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” ๊ณต์ด ๋“ค์–ด์žˆ์ง€ ์•Š์œผ๋ฉฐ, ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” ๊ณต์„ 1๊ฐœ๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

๋„ํ˜„์ด๋Š” ์•ž์œผ๋กœ M๋ฒˆ ๊ณต์„ ๋„ฃ์œผ๋ ค๊ณ  ํ•œ๋‹ค. ๋„ํ˜„์ด๋Š” ํ•œ ๋ฒˆ ๊ณต์„ ๋„ฃ์„ ๋•Œ, ๊ณต์„ ๋„ฃ์„ ๋ฐ”๊ตฌ๋‹ˆ ๋ฒ”์œ„๋ฅผ ์ •ํ•˜๊ณ , ์ •ํ•œ ๋ฐ”๊ตฌ๋‹ˆ์— ๋ชจ๋‘ ๊ฐ™์€ ๋ฒˆํ˜ธ๊ฐ€ ์ ํ˜€์žˆ๋Š” ๊ณต์„ ๋„ฃ๋Š”๋‹ค. ๋งŒ์•ฝ, ๋ฐ”๊ตฌ๋‹ˆ์— ๊ณต์ด ์ด๋ฏธ ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๋“ค์–ด์žˆ๋Š” ๊ณต์„ ๋นผ๊ณ , ์ƒˆ๋กœ ๊ณต์„ ๋„ฃ๋Š”๋‹ค. ๊ณต์„ ๋„ฃ์„ ๋ฐ”๊ตฌ๋‹ˆ๋Š” ์—ฐ์†๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๊ณต์„ ์–ด๋–ป๊ฒŒ ๋„ฃ์„์ง€๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, M๋ฒˆ ๊ณต์„ ๋„ฃ์€ ์ดํ›„์— ๊ฐ ๋ฐ”๊ตฌ๋‹ˆ์— ์–ด๋–ค ๊ณต์ด ๋“ค์–ด ์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N (1 ≤ N ≤ 100)๊ณผ M (1 ≤ M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค.

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๊ณต์„ ๋„ฃ๋Š” ๋ฐฉ๋ฒ•์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋ฐฉ๋ฒ•์€ ์„ธ ์ •์ˆ˜ i j k๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, i๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ j๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ๊นŒ์ง€์— k๋ฒˆ ๋ฒˆํ˜ธ๊ฐ€ ์ ํ˜€์ ธ ์žˆ๋Š” ๊ณต์„ ๋„ฃ๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 2 5 6์€ 2๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ 5๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ๊นŒ์ง€์— 6๋ฒˆ ๊ณต์„ ๋„ฃ๋Š”๋‹ค๋Š” ๋œป์ด๋‹ค. (1 ≤ i ≤ j ≤ N, 1 ≤ k ≤ N)

๋„ํ˜„์ด๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๊ณต์„ ๋„ฃ๋Š”๋‹ค.

5 4
1 2 3
3 4 4
1 4 1
2 2 2

์ถœ๋ ฅ

1๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ N๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ณต์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค. ๊ณต์ด ๋“ค์–ด์žˆ์ง€ ์•Š์€ ๋ฐ”๊ตฌ๋‹ˆ๋Š” 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

1 2 1 1 0

์ž…๋ ฅ์ค‘ ๋งจ ์œ—์ค„์ด ์ฃผ์–ด์ง„ ๋ฐ”๊ตฌ๋‹ˆ ๊ฐฏ์ˆ˜ (N), ๊ณต ๋„ฃ๊ธฐ ๋ฐ˜๋ณต ํšŸ์ˆ˜ (=์ž…๋ ฅ line์˜ ๋ฐ˜๋ณต ํšŸ์ˆ˜ M)

์ด๊ณ , ๋‚˜๋จธ์ง€ ์ž…๋ ฅ์ด i๋ฒˆ๋ถ€ํ„ฐ j๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์— k๋ผ๋Š” ์ˆซ์ž๊ฐ€ ์“ฐ์—ฌ์ง„ ๊ณต์„ ๋„ฃ๋Š”๋‹ค๋Š”๋œป์ด๋‹ค.

 

import java.io.*;
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 N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        int[] array = new int[N];

        for (int l = 0; l < M; l ++ ){
            StringTokenizer aitem = new StringTokenizer(br.readLine());
            int i = Integer.parseInt(aitem.nextToken());
            int j = Integer.parseInt(aitem.nextToken());
            int k = Integer.parseInt(aitem.nextToken());

            for (int basket = i; basket <= j; ++basket){
                array[basket-1] = k;
            }
        }
        for (int h = 0; h < N; h ++ ){
            System.out.print((array[h] + " "));
        }
    }
}

 

๋จผ์ € ๊ฒฐ๊ณผ ๊ฐ’์„ ๋‹ด์„ ๋ฐฐ์—ด array๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

 

for ๋ฌธ์•ˆ์— for๋ฌธ (์ด์ค‘ for๋ฌธ)์„ ๋งŒ๋“ค์–ด์„œ

i ~ j๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜์˜€๋‹ค.

(๋ฐ˜๋ณตํ•˜๋ฉฐ array์— ๊ฐ’์„ ๋‹ด์Œ)

 

๊ทธ ํ›„ array์˜ ์š”์†Œ๋“ค์„ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค.


10813 ๊ณต ๋ฐ”๊พธ๊ธฐ


 

๋ฌธ์ œ

๋„ํ˜„์ด๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ด N๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ ธ ์žˆ๋‹ค. ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” ๊ณต์ด 1๊ฐœ์”ฉ ๋“ค์–ด์žˆ๊ณ , ์ฒ˜์Œ์—๋Š” ๋ฐ”๊ตฌ๋‹ˆ์— ์ ํ˜€์žˆ๋Š” ๋ฒˆํ˜ธ์™€ ๊ฐ™์€ ๋ฒˆํ˜ธ๊ฐ€ ์ ํžŒ ๊ณต์ด ๋“ค์–ด์žˆ๋‹ค.

๋„ํ˜„์ด๋Š” ์•ž์œผ๋กœ M๋ฒˆ ๊ณต์„ ๋ฐ”๊พธ๋ ค๊ณ  ํ•œ๋‹ค. ๋„ํ˜„์ด๋Š” ๊ณต์„ ๋ฐ”๊ฟ€ ๋ฐ”๊ตฌ๋‹ˆ 2๊ฐœ๋ฅผ ์„ ํƒํ•˜๊ณ , ๋‘ ๋ฐ”๊ตฌ๋‹ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ณต์„ ์„œ๋กœ ๊ตํ™˜ํ•œ๋‹ค.

๊ณต์„ ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ€์ง€๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, M๋ฒˆ ๊ณต์„ ๋ฐ”๊พผ ์ดํ›„์— ๊ฐ ๋ฐ”๊ตฌ๋‹ˆ์— ์–ด๋–ค ๊ณต์ด ๋“ค์–ด์žˆ๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N (1 ≤ N ≤ 100)๊ณผ M (1 ≤ M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค.

๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ์„œ ๊ณต์„ ๊ตํ™˜ํ•  ๋ฐฉ๋ฒ•์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋ฐฉ๋ฒ•์€ ๋‘ ์ •์ˆ˜ i j๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, i๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์™€ j๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ณต์„ ๊ตํ™˜ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค. (1 ≤ i ≤ j ≤ N)

๋„ํ˜„์ด๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๊ณต์„ ๊ตํ™˜ํ•œ๋‹ค.

5 4
1 2
3 4
1 4
2 2

์ถœ๋ ฅ

1๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ N๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ณต์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

3 1 4 2 5

์‰ฝ๋‹ค. ์ฒซ๋ฒˆ์งธ ์ค„์€ N๊ฐœ์˜ ๋ฐ”๊ตฌ๋‹ˆ, ๋ช‡๋ฒˆ(M) ๋ฐ˜๋ณตํ• ๊ฒƒ์ธ์ง€๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ 

๊ทธ ๋‹ค์Œ์˜ ์ค„๋ถ€ํ„ฐ i, j์˜ ๊ณต์„ ์„œ๋กœ ๊ตํ™˜ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

import java.io.*;
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 N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());

        int[] array = new int[N];
        for (int n = 0; n < N; n++) {
            array[n] = n+1;
        }

        int temp = 0;
        for (int l = 0; l < M; l++) {
            StringTokenizer item = new StringTokenizer(br.readLine());
            int i = Integer.parseInt(item.nextToken());
            int j = Integer.parseInt(item.nextToken());
            temp = array[i-1];
            array[i-1] = array[j-1];
            array[j-1] = temp;
        }

        for (int h = 0; h < N; h++) {
            System.out.print(array[h] + " ");
        }
    }
}

 

temp๋ฅผ ์ด์šฉํ•ด i๋ฒˆ์งธ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , j์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•