반응형

문제 출처

https://programmers.co.kr/learn/courses/30/lessons/17682

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

문제 풀이

if else문을 떡칠해서 풀이했다. 단순히 문제의 조건에 따라 구현해주면 되는 문제라 어렵지 않았다. 단지 이 문제에서 가장 중요했던 점은 2번 조건인 '각 기회마다 얻을 수 있는 점수는 0점에서 10점까지이다.' 이 문항이다.

dartResult의 1자리씩 끊어서 보다보니까 10이 나온경우 for문의 반복변수를 1개 추가해줘야하기 때문이다.

 

소스 코드

https://github.com/sw93/algorithm/blob/master/Programmers/kakao2018%20-%20%EB%B9%84%EB%B0%80%EC%A7%80%EB%8F%84.cpp

 

sw93/algorithm

problem solve. Contribute to sw93/algorithm development by creating an account on GitHub.

github.com

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

 

반응형

+ Recent posts