본문 바로가기
PS/프로그래머스

프로그래머스 다트게임 cpp

by 지기_ 2021. 5. 28.
반응형

1. 문제

 

2. 설계

 

3. 알고리즘

 

4. 구현

#include <bits/stdc++.h>
using namespace std;

bool isNumber(char c){
    if(c-'0'>=0 && c-'0'<10)    return true;
    else return false;
}

int solution(string dartResult) {
    stack<int> sk;
    int answer = 0;
    for(int i=0; i<dartResult.size(); i++){
        
        if(dartResult[i]=='*'){
            int cur = sk.top();
            sk.pop();
            int prev = sk.top();
            sk.pop();
            cur*=2;
            prev*=2;
            sk.push(prev);
            sk.push(cur);
        }
        if(dartResult[i]=='#'){
            int cur = sk.top();
            sk.pop();
            cur = cur*(-1);
            sk.push(cur);
        }
        if(isNumber(dartResult[i])){
            if(dartResult[i+1]=='0'){
                if(dartResult[i+2]=='S'){
                    sk.push(10);
                    }
                if(dartResult[i+2]=='D'){
                    sk.push(100);
                }
                if(dartResult[i+2]=='T'){
                    sk.push(1000);
                }
                i++;
                }
                else{
                    
            if(dartResult[i+1]=='S'){
                sk.push((dartResult[i]-'0'));
            }
            if(dartResult[i+1]=='D'){
                sk.push((dartResult[i]-'0')*(dartResult[i]-'0'));
            }
            if(dartResult[i+1]=='T'){
                sk.push((dartResult[i]-'0')*(dartResult[i]-'0')*(dartResult[i]-'0'));
            }}
            i++;
        }
    }
    while(!sk.empty()){
        int cur =sk.top();
        answer+=cur;
        sk.pop();
    }
    return answer;
}

 

 

반응형

댓글