HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • 백엔드
  • 프로그래머스C++
  • 백준C++
  • 알고스팟Python
  • 프로그래머스
  • 개발자
  • 데이터베이스
  • back-end
  • 백준Python
  • 프로그래밍
  • Algospot
  • 코딩
  • 백준Go
  • Baekjoon Online Judge
  • front-end
  • 백준
  • 프로그래머스Python
  • 알고스팟
  • programmers
  • Database

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

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

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

2020. 6. 6. 19:41

(주)그렙

[programmers] 프로그래머스 [1차] 다트 게임

(C++)

(글쓴날 : 2020.06.06)

 


* programmers, 프로그래머스 문제 C++ 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

프로그래머스 [1차] 다트 게임


1) 문제

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

 

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

 

programmers.co.kr


2) 풀이 과정

* 시간 복잡도 : O(n)

 

다트를 던진 결과가 주어질 때, 문제에서 요구하는 특정 규칙인 스타상과 아차상을 고려해 최종 점수를 구하는 문제입니다.

 

저의 경우, 스택을 적용했으며, C++을 사용했습니다.

스타상과 아차상의 효과를 적용하기 위해 스택을 이용하여 계산된 점수들을 누적해 두었고, 모든 점수 계산이 완료되었을 때, 스택에 누적된 점수들을 전부 합하여 문제를 해결했습니다.


3) 코드

 

* C++ 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <string>
#include <stack>
#include <cmath>
 
using namespace std;
 
int solution(string dr)
{
    int sum = 0;
    stack<int> s;
    int temp = 0;
    int temp2 = 0;
 
    for (int i = 0; i < dr.size(); i++)
    {
        if (dr[i] == '1')
        {
            s.push(dr[i] - '0');
 
            if (dr[i + 1] == '0')
            {
                s.pop();
                s.push(10);
                i++;
            }
 
            if (dr[i + 1] == 'D')
            {
                temp = s.top();
                s.pop();
                s.push(pow(temp, 2));
            }
 
            else if (dr[i + 1] == 'T')
            {
                temp = s.top();
                s.pop();
                s.push(pow(temp, 3));
            }
        }
        else
        {
            s.push(dr[i] - '0');
 
            if (dr[i + 1] == 'D')
            {
                temp = s.top();
                s.pop();
                s.push(pow(temp, 2));
            }
            else if (dr[i + 1] == 'T')
            {
                temp = s.top();
                s.pop();
                s.push(pow(temp, 3));
            }
        }
        i++;
 
        if (dr[i + 1] == '*' || dr[i + 1] == '#')
        {
            if (dr[i + 1] == '*')
            {
                if (s.size() == 1)
                {
                    temp = s.top();
                    s.pop();
                    s.push(temp * 2);
                }
                else
                {
                    temp2 = s.top();
                    s.pop();
                    temp = s.top();
                    s.pop();
                    temp *= 2;
                    s.push(temp);
                    temp2 *= 2;
                    s.push(temp2);
                }
            }
            else
            {
                temp = s.top();
                s.pop();
                s.push(-temp);
            }
            i++;
        }
    }
 
    while (!s.empty())
    {
        sum += s.top();
        s.pop();
    }
 
    return sum;
}

 

저작자표시 비영리 변경금지 (새창열림)

'Deprecated' 카테고리의 다른 글

[programmers] 프로그래머스 카카오프렌즈 컬러링북(C++)  (0) 2020.06.07
[programmers] 프로그래머스 스킬트리(Python)  (0) 2020.06.07
[programmers] 프로그래머스 실패율(C++)  (0) 2020.06.06
[programmers] 프로그래머스 [1차] 비밀지도(C++)  (0) 2020.06.06
[programmers] 프로그래머스 예산 Level 1(C++)  (0) 2020.06.06
    'Deprecated' 카테고리의 다른 글
    • [programmers] 프로그래머스 카카오프렌즈 컬러링북(C++)
    • [programmers] 프로그래머스 스킬트리(Python)
    • [programmers] 프로그래머스 실패율(C++)
    • [programmers] 프로그래머스 [1차] 비밀지도(C++)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바