๊ธฐ๋ก๋ฐฉ

BOJ_1431 : ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1431 : ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ

Soom_1n 2022. 11. 12. 17:37

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

 

1431๋ฒˆ: ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ

์ฒซ์งธ ์ค„์— ๊ธฐํƒ€์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ๊ฐ€ ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์‹œ๋ฆฌ์–ผ ๋ฒˆํ˜ธ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 50์ด๊ณ , ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž ๋˜๋Š” ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด

www.acmicpc.net



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

  • ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์„ธ ์กฐ๊ฑด์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.
  1. ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ์งง์€ ๊ฒƒ์ด ์•ž์— ์˜จ๋‹ค.
  2. ๊ธธ์ด๊ฐ€ ๊ฐ™๋‹ค๋ฉด, ํ•œ ๋ฌธ์ž์—ด ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ์ˆซ์ž์˜ ํ•ฉ์ด ์ž‘์€ ๊ฒƒ์ด ๋จผ์ € ์˜จ๋‹ค.
  3. ์ˆซ์ž์˜ ํ•ฉ๋„ ๊ฐ™๋‹ค๋ฉด, ๋ฌธ์ž์—ด์„ ์‚ฌ์ „์ˆœ ๋น„๊ตํ•œ๋‹ค.

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        String[] arr = new String[n];
        for (int i = 0; i < n; i++) arr[i] = br.readLine();

        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int len1 = o1.length();
                int len2 = o2.length();
                if (len1 != len2)
                    return len1 - len2;
                else {
                    int sum1 = 0, sum2 = 0;
                    for (int i = 0; i < len1; i++){
                        if (o1.charAt(i) >= '0' && o1.charAt(i) <= '9')
                            sum1 += o1.charAt(i) - '0';
                        if (o2.charAt(i) >= '0' && o2.charAt(i) <= '9')
                            sum2 += o2.charAt(i) - '0';
                    }

                    if (sum1 != sum2)
                        return sum1 - sum2;
                    else
                        return o1.compareTo(o2);
                }
            }
        });

        for (String s : arr) {
            System.out.println(s);
        }
    }
}

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

  • String ๋ฐฐ์—ด arr์— ๋ฌธ์ž์—ด๋“ค์„ ์ž…๋ ฅ๋ฐ›๊ณ  Arrays.sort()๋ฅผ ์‚ฌ์šฉํ•ด ์ •๋ ฌํ•œ๋‹ค.
    • ์ •๋ ฌ ์กฐ๊ฑด์„ orverride๋กœ ์žฌ์ •์˜ํ–ˆ๋‹ค. (์Œ์ˆ˜ ๋ฐ˜ํ™˜ == o1์ด ๋น ๋ฅด๋‹ค)
      • ๋‘ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅด๋ฉด  ์งง์€๊ฒŒ ๋” ๋น ๋ฅด๋‹ค
      • ๋‘ ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ์ˆซ์ž๋“ค์˜ ์ด ํ•ฉ์ด ์ž‘์€์ชฝ์ด ๋” ๋น ๋ฅด๋‹ค.
      • ์œ„ ๋‘ ์กฐ๊ฑด์œผ๋กœ๋„ ํŒ๋ณ„์ด ์•ˆ๋‚˜๋ฉด compareTo()๋ฅผ ์‚ฌ์šฉํ•ด ์‚ฌ์ „์‹ ์ •๋ ฌํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ด๋ฒˆ ์ •๋ ฌ 3๋ฌธ์ œ ํ’€๊ธฐ๋Š” Arrays.sort()์˜ ์ •๋ ฌ ์กฐ๊ฑด์„ ๋‹ค์–‘ํ•˜๊ฒŒ ๋ฐ”๊ฟ”๋ณด๋Š” ์ข‹์€ ์—ฐ์Šต์ด ๋œ ๊ฒƒ ๊ฐ™๋‹ค.
  • ํด๋ž˜์Šค๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ๋‹ค์–‘ํ•œ ์ธ์ž๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์ด ๋ฌธ์ œ์ฒ˜๋Ÿผ ๋‚ด์žฅํ˜•์œผ๋กœ ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์ด์šฉํ•ด ์ •๋ ฌ ์กฐ๊ฑด์„ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์„ ํ™•์‹คํžˆ ์ตํ˜€๊ฐ€๋Š” ๊ฒƒ ๊ฐ™๋‹ค.
    • ๋‚ด์žฅํ˜•์œผ๋กœ ์ •๋ ฌ์กฐ๊ฑด์„ ๋ฐ”๊ฟ€๋•Œ๋Š” ์ •๋ ฌ ์ธ์ž๊ฐ€ 1๊ฐœ๋กœ ๋˜‘๊ฐ™์„ ๋•Œ๋งŒ ๊ฐ€๋Šฅํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

728x90