๐Ÿ’ป๋ฌธ์ œ ํ’€์ด - [Java] 11718. ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ

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

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
6/37

๐Ÿ“๋ฌธ์ œ ์ถœ์ฒ˜ : https://www.acmicpc.net/problem/11718
๐Ÿ“code ์ €์žฅ์†Œ : hayannn/CodingTest_Java_NKLCBHub/#11718 ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ


๐Ÿค” ๋ฌธ์ œ ์š”์•ฝ

์ž…๋ ฅ๋ฐ›์€๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๊ตฌํ˜„ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
๋‹จ, ์กฐ๊ฑด์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  • ์กฐ๊ฑด
    • ๋ฌธ์žฅ์€ ์ตœ๋Œ€ 100์ค„ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๊ฐ ์ค„์€ 100๊ธ€์ž๊ฐ€ ๋„˜์ง€ ์•Š์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • ๋น„์–ด์žˆ๋Š” ์ค„์„ ์ž…๋ ฅํ•  ์ˆ˜ ์—†๊ณ , ๊ณต๋ฐฑ์œผ๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋๋‚ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
    • ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ๊ณต๋ฐฑ, ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“ ์˜ˆ์ œ ์ž…๋ ฅ ๋ถ„์„

  • Case 1 : ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์žฅ์ด ์•ž์„œ ์ œ์‹œํ•œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

๐Ÿšจ ์ฃผ์˜์‚ฌํ•ญ

  • ๋ฌธ์žฅ์„ ๊ตฌ๋ถ„์ง“๋Š” ๊ธฐ์ค€์„ ๋ช…ํ™•ํžˆ ์„ธ์šฐ์ง€ ์•Š์œผ๋ฉด ํ…Œ์ŠคํŠธ์— ์‹คํŒจํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.



1๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜: hasNextLine() ์‚ฌ์šฉ

๐Ÿ“ ํ’€์ด ๊ณผ์ • ๋ฐ ์ฝ”๋“œ

์ˆœ์„œ

  • ๋ฌธ์ž์—ด ์ž…๋ ฅ๋ฐ›๊ธฐ
    • ๋ฌธ์ž์—ด์ด ์—”ํ„ฐ ์‹œ์—๋„ ์ž…๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•ด scanner.hasNextLine() ์‚ฌ์šฉ
    • ์กฐ๊ฑด ์ž‘์„ฑ
  • ๋ฌธ์ž์—ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            if (scanner.hasNextLine()) {
                String words = scanner.nextLine();

                if (words.length() > 100) {
                    System.out.println("100๊ธ€์ž๊ฐ€ ๋„˜์œผ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.");
                } else if (words.matches("^[a-zA-Z\\s0-9]+$")) {
                    System.out.println(words);
                } else {
                    System.out.println("๋ฌธ์ž์—ด ํ˜•์‹์ด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
                }
            } else {
                break;
            }
        }
        scanner.close();
    }
}

์ฝ”๋“œ ์„ค๋ช…

  • hasNextLine
    • NextLine๊ณผ hasNextLine์˜ ์ฐจ์ด
      Java Platform: Class Scanner
      NextLinehasNextLine
      ๊ธฐ์ค€Enter ์ด์ „๊นŒ์ง€๋งŒEnter ์ดํ›„์— ๋‹ค์Œ ์ค„ ์ž…๋ ฅ์ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€ ํŒŒ์•…(True/False)
      ํ˜•ํƒœStringBoolean
  • words.matches("^[a-zA-Z\\s0-9]+$")
    • ์ •๊ทœํ‘œํ˜„์‹์„ ์ด์šฉํ•ด ๋ฌธ์ œ์˜ ์กฐ๊ฑด์— ๋ฒ—์–ด๋‚˜๋Š”(ex.ํŠน์ˆ˜๋ฌธ์ž)๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ฒฝ์šฐ, ๋ฌธ์žฅ์ด ์ถœ๋ ฅ๋˜์ง€ ์•Š๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • matches๋Š” ํŠน์ • ๋ฌธ์ž์—ด ์ฆ‰, ์ •๊ทœํ‘œํ˜„์‹์œผ๋กœ ํ‘œํ˜„ํ•œ ํŠน์ • ๋ฌธ์ž์˜ ์กฐ๊ฑด์„ ๋งค์นญํ•˜๊ธฐ ์œ„ํ•ด ํ•จ๊ป˜ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.



๐Ÿ–จ๏ธ ๊ฒฐ๊ณผ ๋ถ„์„

hasNextLine() ์‚ฌ์šฉ

  • ๋ฉ”๋ชจ๋ฆฌ : 18148KB
  • ์‹œ๊ฐ„ : 252ms
  • ์ฝ”๋“œ ๊ธธ์ด : 721B

โžก๏ธ ๋ฌธ์ž์—ด์„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ด์ง€๋งŒ, ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋ชจ๋‘ ์ถฉ์กฑํ•ด์•ผํ•˜๋Š” ์ฃผ์˜ ์‚ฌํ•ญ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์กฐ๊ฑด์— ๋งž๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌ์„ฑํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

โžก๏ธ hasNextLine()์„ ์ด์šฉํ•ด ์ตœ๋Œ€ 100์ค„์„ ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค๋Š” ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜๊ณ , ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ matches๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์—ด์˜ ๊ตฌ์„ฑ ์กฐ๊ฑด์„ ์ถฉ์กฑํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

โžก๏ธ ๊ทธ๋Ÿฌ๋‚˜ Scanner์˜ ๊ฒฝ์šฐ close๋ฅผ ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๋‚ญ๋น„๊ฐ€ ์ƒ๊ธฐ๊ธฐ ๋•Œ๋ฌธ์— BufferedReader๋ฅผ ์ด์šฉํ•ด์„œ๋„ ํ’€์–ด๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

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

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