굥뷰를 햡시댜

[2017_Blind_Kakao] 다트 게임 본문

알고리즘 문제 풀이/2018 Kakao Blind 문제 풀이

[2017_Blind_Kakao] 다트 게임

GodZ 2019. 7. 29. 17:16

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

 

코딩테스트 연습 - [1차] 다트 게임 | 프로그래머스

 

programmers.co.kr

 

이전의 비밀지도 문제와 같이 문자열을 처리하는 문제이다.

 

문자열의 각 문자마다 의미하는 바가 다르기 때문에 이 부분만 문제에서 설명해준 대로 잘 구현해주면 된다.

 

이런 종류의 문제풀이가 익숙하지 않아 디버깅 하는데 시간이 조금 걸렸다.

 

#include <iostream>
#include <string>

using namespace std;

int solution(string dartResult) {
    int answer = 0;
    
    cin >> dartResult;

	string copy_dartResult = dartResult;

	int score[3] = { 0, };

	int index_cnt = 0;

	for (int i = 0; i < dartResult.length(); i++) {
		if (dartResult[i] - 48 >= 0 && dartResult[i] - 48 <= 10) {
			if (dartResult[i] - 48 == 1) {
				if (dartResult[i + 1] - 48 == 0) {
					score[index_cnt] = 10;
					i += 1;
				}
				else {
					score[index_cnt] = 1;
				}
			}
			else {
				score[index_cnt] = dartResult[i] - 48;
			}
			index_cnt += 1;
		}
	}

	int score_cnt = -1;

	for (int j = 0; j < copy_dartResult.length(); j++) {
		if (copy_dartResult[j] - 48 >= 0 && copy_dartResult[j] - 48 <= 10) {
			if (copy_dartResult[j] - 48 == 1) {
				if (copy_dartResult[j + 1] - 48 == 0) {
					score_cnt += 1;
					j += 1;
				}
				else score_cnt += 1;
			}
			else score_cnt += 1;
			continue;
		}
		if (copy_dartResult[j] == 'S') {

		}
		else if (copy_dartResult[j] == 'D') {
			score[score_cnt] = score[score_cnt] * score[score_cnt];
		}
		else if (copy_dartResult[j] == 'T') {
			score[score_cnt] = score[score_cnt] * score[score_cnt] * score[score_cnt];
		}
		else if (copy_dartResult[j] == '*') {
			if (score_cnt == 0) {
				score[score_cnt] *= 2;
			}
			else {
				score[score_cnt] *= 2;
				score[score_cnt - 1] *= 2;
			}
		}
		else if (copy_dartResult[j] == '#') {
			score[score_cnt] *= -1;
		}
	}

	for (int l = 0; l < 3; l++) {
		answer += score[l];
	}
    
    return answer;
}

 

Comments