2 minute read

πŸ“Œ λ‚œμ΄λ„

πŸ₯ˆ Silver 4


πŸ“Œ 문제

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

image

image


πŸ“Œ 풀이

처음 κ΅¬ν˜„μ„ κ΅¬μƒν• λ•Œ StringTokenizerμ—μ„œ 배열을 λ°›μ•„μ„œ λ„£λŠ” 것 κΉŒμ§€λŠ” μˆ˜μ›”ν•˜κ²Œ ν•˜μ˜€λ‹€. 이후 받은 stλ₯Ό μƒˆλ‘œμš΄ ν•¨μˆ˜μ— λ„£μ–΄μ„œ 3개λ₯Ό λ”ν•œ 1의 μžλ¦¬κ°€ κ°€μž₯ 크게 λ‚˜μ˜€λŠ” 숫자λ₯Ό κ΅¬ν•œν›„ returnν•˜κ²Œ κ΅¬μƒν•˜μ˜€λ‹€.

ν•˜μ§€λ§Œ μ΄μƒν•˜κ²Œ 였λ₯˜κ°€ λ°œμƒν•΄μ„œ λ΄€λŠ”λ° μ•Œκ³ λ³΄λ‹ˆ 1의 μžλ¦¬κ°€ κ°€μž₯ ν¬λ©΄μ„œ κ°€μž₯ 큰 숫자λ₯Ό printν•˜λŠ”μ€„ μ•Œμ•˜λŠ”λ° μˆ«μžκ°€ κ°™μœΌλ©΄ κ°€μž₯ 큰 번호의 번호λ₯Ό 좜λ ₯ν•˜λŠ” κ²ƒμ΄μ˜€λ‹€.

이후 맞게 μˆ˜μ •μ„ ν–ˆμ§€λ§Œ μ•Œκ³ λ¦¬μ¦˜ μžμ²΄μ—μ„œ 큰 λ¬Έμ œκ°€ 생겼닀. NumberGame() λŠ” 5개의 숫자λ₯Ό λ°›μ•„μ„œ 배열에 넣은후 3개λ₯Ό λ”ν•΄μ„œ 1의 μžλ¦¬κ°€ κ°€μž₯ 큰 숫자λ₯Ό returnν•˜λŠ” ν•¨μˆ˜μΈλ°, 5개의 λΉˆκ³΅κ°„μ€‘ 3개의 μžλ¦¬κ°€ μžˆλ‹€λ©΄ λͺ¨λ“  반볡문의 μ΅œλŒ€κ°€ 3이고, μ•žμ˜ μžλ¦¬κ°€ λ’€λ‘œ 갈수둝 λ’€μ˜ μˆ«μžλ“€μ˜ μžλ¦¬κ°€ 1κ°œμ”© μ€„μ–΄λ“œλ―€λ‘œ λ‹€μŒκ³Ό 같이 ꡬ상을 ν–ˆμ—ˆλ‹€.

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3 - i; j++) {
        for (int k = 0; k < 3 - j; k++) {
            current = cards[i] + cards[j] + cards[k];
            if (current % 10 > max % 10) {
                max = current;
            } else if (current % 10 == max % 10 && current > max) {
                max = current;
            }
        }
    }
}

ν•˜μ§€λ§Œ μ‹€μž¬λ‘œ μž‘λ™μ„ ν•΄λ³΄λ‹ˆ, 각 μžλ¦¬λ§ˆλ‹€ 000 ~ 333 의 κ³΅κ°„λ§Œμ„ μ‚¬μš©ν•  수 μžˆλŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€. λ”°λΌμ„œ μ΅œμ’…μ μœΌλ‘œ λ‹€μŒκ³Ό 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•˜μ˜€λ‹€.

ν˜Ήμ‹œλΌλ„ μΆ”κ°€ μ˜ˆμ œκ°€ ν•„μš”ν•˜λ©΄ λ‹€μŒ μ˜ˆμ œλ„ μ μš©ν•΄λ΄λ„ λœλ‹€.

3
7 5 5 4 9
1 1 1 1 1
7 5 5 4 9

λ‹΅ : 3


πŸ“Œ Code

package Implements;

import java.io.*;
import java.util.StringTokenizer;

public class N2303 {
    public static int[] nums;
    private static int i;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter((System.out)));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int num = Integer.parseInt(st.nextToken());

        nums = new int[num];

        for (int i = 0; i < num; i++) {
            st = new StringTokenizer(br.readLine());
            nums[i] = NumberGame(st);
        }

        int max = 0;
        int flag = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] % 10 >= max % 10) {
                max = nums[i];
                flag = i;
            }
        }
        System.out.println(flag + 1);
    }
    public static int NumberGame(StringTokenizer st) {
        int[] cards = new int[5];
        int index = 0;

        while (st.hasMoreTokens()) {
            cards[index++] = Integer.parseInt(st.nextToken());
        }

        int max = 0;
        int current;
        for (int i = 0; i < 3; i++) {
            for (int j = i + 1; j < 4; j++) {
                for (int k = j + 1; k < 5; k++) {
                    current = cards[i] + cards[j] + cards[k];
                    if (current % 10 > max % 10) {
                        max = current;
                    } else if (current % 10 == max % 10 && current > max) {
                        max = current;
                    }
                }
            }
        }
        return max;
    }
}

package (이름); λ₯Ό λ•Œκ³ , class 이름을 Main으둜 λ³€κ²½ν•˜λ©΄ λœλ‹€.



개인 곡뢀 기둝용 λΈ”λ‘œκ·Έμž…λ‹ˆλ‹€.
ν‹€λ¦¬κ±°λ‚˜ 였λ₯˜κ°€ μžˆμ„ 경우 μ œλ³΄ν•΄μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€.😁