๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ์‚ฌ์น™์—ฐ์‚ฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ SWEA #1233

by seolhee2750 2022. 8. 11.
๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV141176AIwCFAYD 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution {
 
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        StringBuffer out = new StringBuffer();
         
        for(int t = 1; t <= 10; t++) {
            int n = Integer.parseInt(in.readLine());
            int answer = 1;         
             
            for(int i = 0; i < n; i++) {
                st = new StringTokenizer(in.readLine());
                if(answer == 0) continue; // answer๊ฐ€ 0์ด๋ฉด ๋‹ค์Œ ์ž…๋ ฅ๋ถ€ํ„ฐ ๊ฒ€์‚ฌํ•  ํ•„์š”x
                st.nextToken(); // ๋…ธ๋“œ ๋ฒˆํ˜ธ ์•Œ ํ•„์š”x => ํŒจ์Šค
                String cal = st.nextToken();
                 
                if(st.hasMoreTokens()) { // ์ž…๋ ฅ ๋‚จ์•˜์„ ๋–ผ == ๋‹จ๋ง ๋…ธ๋“œ๊ฐ€ ์•„๋‹ ๋•Œ == ์ˆซ์ž๋ฉด ์•ˆ๋ผ !
                    if(!(cal.equals("+") || cal.equals("-") || cal.equals("*") || cal.equals("/"))) answer = 0;
                } else { // ๋” ์ด์ƒ ์ž…๋ ฅ ์—†์„ ๋•Œ == ๋‹จ๋ง ๋…ธ๋“œ์ผ ๋•Œ == ์—ฐ์‚ฐ์ž๋ฉด ์•ˆ๋ผ !
                    if(cal.equals("+") || cal.equals("-") || cal.equals("*") || cal.equals("/")) answer = 0;
                }
            }
            out.append("#" + t + " " + answer + "\n");
        }
        System.out.println(out);
    }
}

๐Ÿ‘‰ ๊ตฌํ˜„

์—ฐ์‚ฐ์ด ์„ฑ๋ฆฝ๋˜๊ณ  ์•ˆ๋˜๊ณ ์˜ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” ๊ธฐ์ค€์ด ๊ต‰์žฅํžˆ ๊ฐ„๋‹จํ•˜๊ธฐ ๋•Œ๋ฌธ์—

๊ตณ์ด ๊ทธ๋ž˜ํ”„๋ฅผ ์ด์šฉํ•˜์—ฌ ๋…ธ๋“œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋Š” ๋“ฑ์˜ ๋™์ž‘์„ ํ•  ํ•„์š” ์—†์ด, ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ๋ฐ”๋กœ ํ™•์ธ์„ ํ–ˆ๋‹ค.

 

โœ”๏ธ ์—ฐ์‚ฐ์ด ์„ฑ๋ฆฝ๋˜์ง€ ์•Š๋Š” ๊ธฐ์ค€

1) ๋‹จ๋ง ๋…ธ๋“œ๋Š” ์—ฐ์‚ฐ์ž๋ฉด ์•ˆ๋œ๋‹ค.

2) ๋‹จ๋ง ๋…ธ๋“œ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์ˆซ์ž๋ฉด ์•ˆ๋œ๋‹ค.

 

[ ํ’€์ด ์„ค๋ช… ]

  • n๋ฒˆ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์ผ๋‹จ ํ•ด๋‹น ์ž…๋ ฅ ๋ผ์ธ์„ ๋ฐ›์•„์˜จ ํ›„, answer์„ ํ™•์ธํ–ˆ๋‹ค.
    ์ž…๋ ฅ์„ ๋ฐ›์œผ๋ฉด์„œ ์—ฐ์‚ฐ์ด ์„ฑ๋ฆฝ๋˜์ง€ ์•Š์Œ์„ ํ™•์ธํ•˜๋ฉด ๋ฐ”๋กœ answer๋ฅผ 0์œผ๋กœ ๋ฐ”๊ฟ”์ค„ ๊ฒƒ์ด๋ฏ€๋กœ
    answer๊ฐ€ 0์ด๋ฉด ๋”์ด์ƒ ์—ฐ์‚ฐ ์„ฑ๋ฆฝ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • answer๊ฐ€ 0์ด ์•„๋‹ˆ๋ผ๋ฉด ์ผ๋‹จ ์ž…๋ ฅ๋œ ๊ฐ’๋“ค์„ ํ™•์ธํ•ด์•ผ ํ•˜๋Š”๋ฐ,
    ์ž…๋ ฅ๋œ ๊ฐ’๋“ค ์ค‘ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์ธ ๋…ธ๋“œ ๋ฒˆํ˜ธ๋Š” ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ฐ›์•„์„œ ๋‚ ๋ ค๋ฒ„๋ ธ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ๊ฐ’์€ ํ•ด๋‹น ๋…ธ๋“œ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์ด๋ฏ€๋กœ, ์—ฐ์‚ฐ์ž์ธ์ง€ ์ˆซ์ž์ธ์ง€ ํŒ๋‹จํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    ๋”ฐ๋ผ์„œ cal๋ณ€์ˆ˜๋กœ ์ž…๋ ฅ๋ฐ›๊ณ , ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์œ„์—์„œ ๋งํ•œ ์—ฐ์‚ฐ์ด ์„ฑ๋ฆฝ๋˜์ง€ ์•Š๋Š” ๊ธฐ์ค€์„ ํ™•์ธํ–ˆ๋‹ค.
  • hasMoreTokens() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž…๋ ฅ์ด ๋‚จ์€์ง€ ํ™•์ธ, ๋‹จ๋ง๋…ธ๋“œ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ํŒ๋‹จํ–ˆ๊ณ ,
    (1)๋‹จ๋ง๋…ธ๋“œ๊ฐ€ ์•„๋‹Œ๋ฐ ์—ฐ์‚ฐ์ž๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ, (2)๋‹จ๋ง๋…ธ๋“œ์ธ๋ฐ ์—ฐ์‚ฐ์ž์ธ๊ฒฝ์šฐ ๋‘ ๊ฒฝ์šฐ์—
    ๋”์ด์ƒ ์ด ํŠธ๋ฆฌ๋Š” ์—ฐ์‚ฐ์ด ์„ฑ๋ฆฝ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๋ณด์•„ answer์„ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ž…๋ ฅ๊ฐ’์ด ๋„˜๋‚˜ ๋ณต์žกํ•ด๋ณด์—ฌ์„œ ๋ฌธ์ œ ์ดํ•ด์— ์‹œ๊ฐ„์„ ๋งŽ์ด ์ผ์ง€๋งŒ,,,! ์‰ฌ์šด ๋ฌธ์ œ์˜€๋‹ค.

๋Œ“๊ธ€