알고리즘/카카오 기출

[프로그래머스/C++] [1차] 다트 게임 - 2018 KAKAO BLIND RECRUITMENT

수디sudy 2022. 2. 11. 18:30

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

 

코딩테스트 연습 - [1차] 다트 게임

 

programmers.co.kr

 

 

 

동진이오빠가 1레벨로 풀재서 푼 다트 게임,,,

 

나는 다트 던지는 1,2,3번의 점수를 각각 벡터에 저장해서 했다

ex) 1D2S3T* = 1D, 2S, 3T* 로 나눠서 저장

 

 

나는 그냥 노가다로 풀었는데 코드가 너무 지저분하다,,,,

 

그렇지만,,,,,,,, 배고프니까 그냥 이렇게 놔두겠어,,,,,,,,

 

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

int solution(string dartResult) {
    int answer = 0;
    vector<string> lst;
    int flag = 0;
    vector<int> score_lst;
    
    for(int i=1; i<dartResult.size(); i++)
    {
        if(dartResult[i] == '0' && dartResult[i-1] == '1')
            continue;
        if(isdigit(dartResult[i]))
        {
            string str = "";
            for(int j=flag; j<i; j++)
            {
                str += dartResult[j];
            }
            lst.push_back(str);
            flag = i;
        }
        if(i == dartResult.size()-1)
        {
            string str = "";
            for(int j=flag; j<=i; j++)
            {
                str += dartResult[j];
            }
            lst.push_back(str);
        }
    }
    
    
    for(int i=0; i<lst.size(); i++)
    {
        int score;
        bool flag = false;
        
        if(lst[i][0] == '1' && lst[i][1] == '0')
        {
            score = 10;
            flag = true;
        }
        else
        {
            score = lst[i][0] - '0';
        }
        
        
        
        if(flag == true)
        {
            if(lst[i][2] == 'D')
                score = pow(score, 2);
            else if(lst[i][2] == 'T')
                score = pow(score, 3);
        }
        else
        {
            if(lst[i][1] == 'D')
                score = pow(score, 2);
            else if(lst[i][1] == 'T')
                score = pow(score, 3);
        }
        
        
        
        if(flag == true && isascii(lst[i][3]))
        {
            if(lst[i][3] == '*')
            {
                if(i == 0)
                    score *= 2;
                else
                {
                    score_lst[i-1] *= 2;
                    score *= 2;
                }
            }
            else if(lst[i][3] == '#')
            {
                score *= -1;
            }
        }
        else if(flag == false && isascii(lst[i][2]))
        {
            if(lst[i][2] == '*')
            {
                if(i == 0)
                    score *= 2;
                else
                {
                    score_lst[i-1] *= 2;
                    score *= 2;
                }
            }
            else if(lst[i][2] == '#')
            {
                score *= -1;
            }
        }
        
        
        
        score_lst.push_back(score);
        
        
    }
    
    for(int i=0; i<score_lst.size(); i++)
    {
        answer += score_lst[i];
    }
    
    
    return answer;
}