4 분 소요

🗒️ [다트 게임]](https://school.programmers.co.kr/learn/courses/30/lessons/17682)

#include <vector>
#include <string>
#include <iostream>
using namespace std;

int solution(string dartResult) {
    vector<int> score;
    int idx = 0;
    while(idx < dartResult.size())
    {
        // 숫자부분 추출
        string str_num="";
        for(;idx<dartResult.size(); idx++)
        {
            char c = dartResult[idx];
            if(c >= '0' && c <='9')
                str_num+=c;
            else
                break;
        }
        cout << str_num << '\n';
        int num = stoi(str_num);
        
        char c = dartResult[idx++];
        
        // 점수영역 계산
        int point=0;
        if(c == 'S')
            point = num;
        else if(c=='D')
            point = num*num;
        else if(c=='T')
            point = num*num*num;
        
        char op = dartResult[idx];
        if(op !='*' && op != '#')
        {
            score.push_back(point);
            continue;
        }
        
        if(op == '*')
        {
            point *=2;
            if(score.size() > 0)
                score.back() *=2;
            score.push_back(point);
        }
        else
        {
            point *= -1;
            score.push_back(point);
        }
        ++idx;
    }
    
    // 총 점수 계산
    int answer=0;
    for(int s:score)
        answer += s;
    return answer;
}

  • while문을 제거할 수 있을 것 같음. 다시한번 풀어보기!

업데이트:

댓글남기기