๐Ÿฅ‰ [Baekjoon / Java] 18406. ๋Ÿญํ‚ค ์ŠคํŠธ๋ ˆ์ดํŠธ

์ดํ•˜์–€ยท2024๋…„ 4์›” 9์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
10/37
post-thumbnail

๐Ÿ’ก Info



๐Ÿ’ญ ๋ฌธ์ œ ์ดํ•ด

  • ํ˜„์žฌ ์ ์ˆ˜ N์ด ์ฃผ์–ด์ง€๋ฉด ๋Ÿญํ‚ค ์ŠคํŠธ๋ ˆ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ์ธ์ง€ ์•„๋‹Œ์ง€ ์•Œ๋ ค์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ
  • ํŠน์ • ์กฐ๊ฑด
    • ํ˜„์žฌ ์บ๋ฆญํ„ฐ ์ ์ˆ˜๋ฅผ N์ด๋ผ ํ•  ๋•Œ ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ์ˆ˜ N์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ  ์™ผ์ชฝ ๋ถ€๋ถ„์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ๊ณผ ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์„ ๋”ํ•œ ๊ฐ’์ด ๋™์ผํ•œ ์ƒํ™ฉ
    • ํ˜„์žฌ ์ ์ˆ˜๊ฐ€ 123,402๋ผ๋ฉด ์™ผ์ชฝ ๋ถ€๋ถ„์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์€ 1 + 2 + 3, ์˜ค๋ฅธ์ชฝ ๋ถ€๋ถ„์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์€ 4 + 0 + 2์ด๋ฏ€๋กœ ๋‘ ํ•ฉ์ด 6์œผ๋กœ ๋™์ผํ•˜์—ฌ ๋Ÿญํ‚ค ์ŠคํŠธ๋ ˆ์ดํŠธ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๐Ÿ“ฅ์ž…๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— ์ ์ˆ˜ N์ด ์ •์ˆ˜๋กœ ์ฃผ์–ด์ง„๋‹ค. (10 โ‰ค N โ‰ค 99,999,999) ๋‹จ, ์ ์ˆ˜ N์˜ ์ž๋ฆฟ์ˆ˜๋Š” ํ•ญ์ƒ ์ง์ˆ˜ ํ˜•ํƒœ๋กœ๋งŒ ์ฃผ์–ด์ง„๋‹ค.
    • ์ž…๋ ฅ ์˜ˆ์‹œ 1
    123402
    • ์ž…๋ ฅ ์˜ˆ์‹œ 2
    7755

๐Ÿ“ค์ถœ๋ ฅ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— ๋Ÿญํ‚ค ์ŠคํŠธ๋ ˆ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด "LUCKY"๋ฅผ, ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๋ฉด "READY"๋ผ๋Š” ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • ์ถœ๋ ฅ ์˜ˆ์‹œ 1
      LUCKY
    • ์ถœ๋ ฅ ์˜ˆ์‹œ 2
      READY


๐Ÿ’ญ ์ƒ๊ฐํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜

์‹ค์ œ ํ’€์ด ์‹œ๊ฐ„ : 17๋ถ„

  • N ์ž…๋ ฅ๋ฐ›๊ธฐ
  • N์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ž๋ฅด๊ธฐ, ๊ฐ ํ•ฉ ๊ตฌํ•˜๊ธฐ
  • ๋‘ ํ•ฉ ๋น„๊ตํ•˜๊ธฐ
    • ๋งŒ์•ฝ, ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•œ๋‹ค๋ฉด LUCKY ์ถœ๋ ฅ
    • ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด READY ์ถœ๋ ฅ
import java.util.*;

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

        //1. N ์ž…๋ ฅ๋ฐ›๊ธฐ
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0; i<N; i++){
            arr[i] = sc.nextInt();
        }

        //2. N์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ž๋ฅด๊ธฐ
        int mid = arr.length / 2;
        int[] first = Arrays.copyOfRange(arr, 0, mid);
        int[] end = Arrays.copyOfRange(arr, mid, arr.length);

        //3. ๊ฐ ํ•ฉ ๊ตฌํ•˜๊ธฐ
        int firstSum = Arrays.stream(first).sum();
        int endSum = Arrays.stream(end).sum();


        //4. ๋‘ ํ•ฉ ๋น„๊ตํ•˜๊ธฐ
        //  - ๋งŒ์•ฝ, ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•œ๋‹ค๋ฉด LUCKY ์ถœ๋ ฅ
        //  - ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด READY ์ถœ๋ ฅ
        if(firstSum == endSum){
            System.out.println("LUCKY");
        } else {
            System.out.println("READY");
        }
    }
}


โŒ ์˜ค๋‹ต์ฒดํฌ

  • N์„ ์ž…๋ ฅ๋ฐ›์„ ๊ฒฝ์šฐ, String์œผ๋กœ ๋ฐ›์•„์•ผ ํ•จ!

        //1. N ์ž…๋ ฅ๋ฐ›๊ธฐ
        String input = sc.nextLine().trim();
        int N = input.length();
        int[] arr = new int[N];
    
        for (int i = 0; i < N; i++) {
            arr[i] = input.charAt(i) - '0';
        }


๐Ÿ’ญ ์ตœ์ข… ํ’€์ด

  • N ์ž…๋ ฅ๋ฐ›๊ธฐ(๋‹จ, String์œผ๋กœ)
  • N์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ž๋ฅด๊ธฐ, ๊ฐ ํ•ฉ ๊ตฌํ•˜๊ธฐ
  • ๋‘ ํ•ฉ ๋น„๊ตํ•˜๊ธฐ
    • ๋งŒ์•ฝ, ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•œ๋‹ค๋ฉด LUCKY ์ถœ๋ ฅ
    • ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด READY ์ถœ๋ ฅ
import java.util.*;

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

        //1. N ์ž…๋ ฅ๋ฐ›๊ธฐ
        String input = sc.nextLine().trim();
        int N = input.length();
        int[] arr = new int[N];

        for (int i = 0; i < N; i++) {
            arr[i] = input.charAt(i) - '0';
        }

        //2. N์„ ์ ˆ๋ฐ˜์œผ๋กœ ์ž๋ฅด๊ธฐ
        int mid = N / 2;
        int[] first = Arrays.copyOfRange(arr, 0, mid);
        int[] end = Arrays.copyOfRange(arr, mid, arr.length);

        //3. ๊ฐ ํ•ฉ ๊ตฌํ•˜๊ธฐ
        int firstSum = Arrays.stream(first).sum();
        int endSum = Arrays.stream(end).sum();


        //4. ๋‘ ํ•ฉ ๋น„๊ตํ•˜๊ธฐ
        //  - ๋งŒ์•ฝ, ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•œ๋‹ค๋ฉด LUCKY ์ถœ๋ ฅ
        //  - ๋‘ ํ•ฉ์ด ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด READY ์ถœ๋ ฅ
        if(firstSum == endSum){
            System.out.println("LUCKY");
        } else {
            System.out.println("READY");
        }
    }
}

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€