[프로그래머스] [1차] 다트 게임(C++)[△]



#include <string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int solution(string dartResult) {
    int answer = 0;
    
    vector<int> tempAnswer;
	for (string::size_type i = 0; i < dartResult.size(); i++) {
		if (dartResult[i] == 'S') {
			if (dartResult[i - 1] == '0' && dartResult[i - 2] == '1') {
				tempAnswer.push_back(10);
				continue;
			}
			tempAnswer.push_back(dartResult[i-1]-'0');

		}
		else if (dartResult[i] == 'D') {
			if (dartResult[i - 1] == '0' && dartResult[i - 2] == '1') {
				tempAnswer.push_back(pow(10,2));
				continue;
			}
			tempAnswer.push_back(pow(dartResult[i-1] - '0',2));
		}
		else if (dartResult[i] == 'T') {
			if (dartResult[i - 1] == '0' && dartResult[i - 2] == '1') {
				tempAnswer.push_back(pow(10, 3));
				continue;
			}
			tempAnswer.push_back(pow(dartResult[i-1] - '0', 3));
		}
		else if (dartResult[i] == '*') {
			if (tempAnswer.size() == 1) {
				tempAnswer[0] *= 2;
			}
			else {
				tempAnswer[tempAnswer.size() - 1] *= 2;
				tempAnswer[tempAnswer.size() - 2] *= 2;
			}
		}
		else if (dartResult[i] == '#') {
			tempAnswer[tempAnswer.size() - 1] *= -1;
		}
	}
	for (vector<int>::size_type i = 0; i < tempAnswer.size(); i++) {
		answer += tempAnswer[i];
	}
    
    return answer;
}

이번 문제는 케이스를 나누는게 중요했다. 다른 사람들의 풀이를 보니 더 좋은 방법이 존재하기에 △를 줬다.




© 2022. by KSC

Powered by sora