본문 바로가기
개발 노트/백준, 프로그래머스 풀이

[프로그래머스 1 / c++] 다트 게임

by tokkiC 2022. 6. 22.

문제의 룰이 길어서 난해하지만, 룰 자체는 복잡하지 않은 문제였다

하지만 구현이 아직 약해서 생각을 코드로 풀이하기가 쉽지 않았다

while (idx<string.length) 라니 (혹은 string.size()) int i++의 for문 만을 사용하던 나에게는

단비같은 도구가 아닐수 없다

저런 풀이 방법도 있었다니 유용하게 써먹자 

 

#include <bits/stdc++.h>

using namespace std;

int solution(string dartResult) {
    int answer = 0;
    vector<int> score;
    int idx=0;
    
    while(idx<dartResult.size()){
        if(dartResult[idx]=='1'){
            if(dartResult[idx+1]=='0'){
                score.push_back(10);
                idx++;
            }
            else {
                score.push_back(1);
                 }
        } else {
          score.push_back(dartResult[idx]-'0');  
        }
        idx++;
        
        if(dartResult[idx]=='S')
            score[score.size()-1] = pow(score[score.size()-1], 1);
        if(dartResult[idx]=='D')
            score[score.size()-1] = pow(score[score.size()-1], 2);
        if(dartResult[idx]=='T')
            score[score.size()-1] = pow(score[score.size()-1], 3);                             idx++;
                                              
        if(dartResult[idx]=='*') {
            score[score.size()-1] *= 2;
            score[score.size()-2] *= 2;
            idx++;
        }
        if(dartResult[idx]=='#') {
            score[score.size()-1] *= -1;
            idx++;
        }
            
    }
    
    for(int i=0; i<score.size(); i++)
        answer += score[i];
    
    return answer;
}

댓글