๊ธฐ๋ก๋ฐฉ

BOJ_1380 : ๊ท€๊ฑธ์ด ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1380 : ๊ท€๊ฑธ์ด

Soom_1n 2022. 9. 22. 17:54

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1380๋ฒˆ: ๊ท€๊ฑธ์ด

์ž…๋ ฅ์€ ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง„ ์‹œ๋‚˜๋ฆฌ์˜ค๋“ค๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค. ์‹œ๋‚˜๋ฆฌ์˜ค ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ฆ๊ฐ€ํ•˜๊ณ , ๊ฐ ์‹œ๋‚˜๋ฆฌ์˜ค๋Š” ์•„๋ž˜์˜ ๋‚ด์šฉ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ํ•œ ์ค„์— ๊ท€๊ฑธ์ด๋ฅผ ์••์ˆ˜๋‹นํ•œ ์—ฌํ•™์ƒ์˜ ์ˆ˜, n (1 ≤ n ≤ 100)์ด

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ํ•™์ƒ๋“ค ์ˆ˜ n, ํ•™์ƒ๋“ค ๋ช…๋‹จ, ๊ท€๊ฑธ์ด ์••์ˆ˜/๋ฐ˜ํ™˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ž…๋ ฅ๋œ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ์— 1๋ฒˆ ๋“ฑ์žฅํ•œ ํ•™์ƒ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„ ์‹œ๋‚˜๋ฆฌ์˜ค ๋ฒˆํ˜ธ์™€ ํ•™์ƒ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  • n์œผ๋กœ 0์ด ์ž…๋ ฅ๋  ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int count = 0;
        sc.nextLine();

        while(n!=0) {
            String student[] = new String[n];
            for (int i = 0; i < n; i++){
                student[i] = sc.nextLine();
            }
            int check[] = new int[n];
            for (int i = 0; i < n*2 - 1; i++){
                String input[] = sc.nextLine().split(" ");
                check[Integer.parseInt(input[0]) - 1]++;
            }

            for (int i = 0; i < n; i++) {
                if (check[i] == 1) {
                    System.out.println(++count + " " + student[i]);
                    break;
                }
            }
            n = sc.nextInt();
            sc.nextLine();
        }
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • n์ด 0์ด ์ž…๋ ฅ ๋  ๋•Œ ๊นŒ์ง€ while๋ฌธ์„ ๋ฐ˜๋ณตํ•œ๋‹ค.
  • ์‹œ๋‚˜๋ฆฌ์˜ค ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ count ๋ณ€์ˆ˜๋กœ ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์„ผ๋‹ค.
  • ํ•™์ƒ ๋ช…๋‹จ์ด๋‚˜ ์••์ˆ˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•œ ์ค„๋กœ ์ž…๋ ฅ๋˜๋ฏ€๋กœ .nextLine()์„ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
    • ๋ฒ„ํผ์— \n์ด ๋‚จ์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ n์„ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ๋„ .nextLline()์„ ์จ์„œ ๋น„์›Œ์ค€๋‹ค.
  • ๋ฆฌ์ŠคํŠธ์— ๋“ฑ์žฅํ•œ ํšŸ์ˆ˜๋ฅผ check ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.
  • ์‹œ๋‚˜๋ฆฌ์˜ค ๋ฒˆํ˜ธ์™€ ๋ฆฌ์ŠคํŠธ์— 1๋ฒˆ ๋“ฑ์žฅํ•œ ํ•™์ƒ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ •๋ง ๊ฐ„๋‹จํžˆ ํ’€์—ˆ๋Š”๋ฐ ํ‹€๋ ธ๋‹ค๊ณ  ๋‚˜์™€์„œ 1์‹œ๊ฐ„์ด๋‚˜ ๊ณ ์‹ฌํ•˜๊ณ  ๋‹ต์ง€๋„ ์ฐพ์•„๋ดค๋‹ค.
  • ๊ฒฐ๊ตญ ์›์ธ์€ ์ž…๋ ฅ๋ฐฉ์‹์— ์žˆ์—ˆ๋Š”๋ฐ, nextLine()๋กœ ์ž…๋ ฅ๋ฐ›์€ ๋ฐ˜ํ™˜ ๋ฆฌ์ŠคํŠธ์—์„œ ์ˆซ์ž๋งŒ ๋นผ๋‚ด์„œ ์“ธ๋•Œ,
    charAt(0)์„ ์‚ฌ์šฉํ–ˆ๋”๋‹ˆ n์ด 10์ด์ƒ์ผ๋•Œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค.
    • Integer.parseInt()๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณ€ํ™˜ํ•˜๋ฉฐ ํ•ด๊ฒฐํ–ˆ๋‹ค.
    • ์•„์ฃผ ์‰ฌ์šด ์˜ค๋ฅ˜์˜€๋Š”๋ฐ ์ฐพ๊ธฐ๋Š” ์ •๋ง ์–ด๋ ค์› ๋‹ค.

728x90