๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL๐Ÿ”ฅ/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[ํ•ญํ•ด99ํด๋Ÿฝ] Java ๋น„๊ธฐ๋„ˆ_Day 3 !!์ดˆ์ฝœ๋ฆฟ ์ค‘๋… ์ฃผ์˜!!

by hk713 2025. 4. 2.

์˜ค๋Š˜์˜ ๋ฌธ์ œ >> 31458๋ฒˆ: !!์ดˆ์ฝœ๋ฆฟ ์ค‘๋… ์ฃผ์˜!!

 

[ ์ƒ๊ฐ ํ๋ฆ„ ]

!๊ฐ€ ์‚ฌ๋ผ์งˆ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์šฐ์„ ์ˆœ์œ„๋Œ€๋กœ ๋ฌธ์ž์—ด์„ ๋Œ€์ฒดํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ!

 

[ Java ]

import java.util.*;

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

        for(int i=0; i<T; i++){
            String word = sc.nextLine();

            while (word.contains("!")) {
                if (word.contains("0!")) {
                    word = word.replace("0!", "1");
                } else if (word.contains("!0")) {
                    word = word.replace("!0", "1");
                } else if (word.contains("1!")) {
                    word = word.replace("1!", "1");
                } else if (word.contains("!1")) {
                    word = word.replace("!1", "0");
                }
            }
            System.out.println(word);
        }
        sc.close();
    }
}

์ฒ˜์Œ ์ˆซ์ž๋ฅผ ์ž…๋ ฅ ๋ฐ›์„ ๋•Œ, sc.nextInt()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐœํ–‰๋ฌธ์ž๋Š” (\n) ๋‚จ์•„์žˆ๊ฒŒ ๋œ๋‹ค..!

๊ทธ๋ž˜์„œ ๊ทธ ๋‹ค์Œ ๋ฌธ์ž์—ด(word)์„ ์ž…๋ ฅ ๋ฐ›๊ธฐ ์ „์— sc.nextLine();์„ ์จ์ฃผ์–ด์•ผ

๋‚จ์•„์žˆ๋Š” ๊ฐœํ–‰๋ฌธ์ž๊ฐ€ ์ฒ˜๋ฆฌ๋˜์–ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ for๋ฌธ ์•ˆ์—์„œ ์ž…๋ ฅ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค~

๋Œ“๊ธ€