Notice
Recent Posts
Recent Comments
Today
Total
01-25 23:16
Link
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Partially Committed

[2018 KAKAO BLIND RECRUITMENT] (1์ฐจ) ๋‹คํŠธ ๊ฒŒ์ž„ ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm || ๋ฌธ์ œํ’€์ด/PS

[2018 KAKAO BLIND RECRUITMENT] (1์ฐจ) ๋‹คํŠธ ๊ฒŒ์ž„

WonderJay 2022. 7. 6. 12:26
728x90
๋ฐ˜์‘ํ˜•
SMALL

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

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๋ฌธ์ œ์—์„œ ์ง€์‹œํ•œ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

 

string ๋ฌธ์ž์—ด๋กœ ์ œ๊ณต๋˜๋Š” dartResult ์— ๋”ฐ๋ผ์„œ ๋‹คํŠธ๋ฅผ 3๋ฒˆ ๋˜์กŒ์„ ๋•Œ์˜ ์ตœ์ข… ๋“์ ์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋˜๋Š”๋ฐ

 

n ๋ฒˆ์งธ ๋‹คํŠธ๋ฅผ ๋˜์กŒ์„ ๋•Œ, n-1 ๋ฒˆ์งธ ๋‹คํŠธ๋ฅผ ๋˜์ ธ ์–ป์€ ์ ์ˆ˜๋ฅผ ๋ฐ”๊พธ์–ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ๋ฐœ์ƒํ•˜๋ฏ€๋กœ

 

stack<int> score ์„ ์„ ์–ธํ•˜์—ฌ ๊ฐ๊ฐ์˜ ํšŒ์ฐจ์— ์–ป์€ ์ ์ˆ˜๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ push ํ•ด์ฃผ์—ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ํ•„์š”์— ๋”ฐ๋ผ score ์˜ top ์— ์ ‘๊ทผํ•˜๊ณ , ์กฐ๊ฑด์— ๋งž๋Š”๋‹ค๋ฉด top ์„ ๊ฐ€์ ธ์™€์„œ ๊ฐ’์„ ์ˆ˜์ •ํ•œ ๋’ค์— ๋‹ค์‹œ push ํ•œ๋‹ค.

 

์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์€ ์ ์ˆ˜์˜ ๋ฒ”์œ„๊ฐ€ 0~10 ์ด๋ฏ€๋กœ 10 ์— ๋Œ€ํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ž˜ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์Šคํƒ€์ƒ์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ๋•Œ, stack ์˜ size ๊ฐ€ 1์ธ ๊ฒฝ์šฐ์— ๋Œ€ํ•ด์„œ๋„ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

[C++]

#include <string>
#include <stack>
using namespace std;

void star(stack<int> & score){
  // ์Šคํƒ€์ƒ
    int point = 0;
    int prev_point = 0;
    
    if(score.size() == 1)
    { // 1ํšŒ์ฐจ
        point = score.top(); // stack ์˜ top ์„ ๊บผ๋‚ด์„œ
        score.pop();
        
        point = point*2; // ์ ์ˆ˜๋ฅผ 2๋ฐฐ๋กœ
        score.push(point);
        return;
    }
    else
    {
        point = score.top(); 
        score.pop();
        
        prev_point = score.top();
        score.pop();
        
        point = point*2;
        prev_point = prev_point*2;
        
        score.push(prev_point);
        score.push(point);
        return;
        
    }
}

void acha(stack<int> & score){
  // ์•„์ฐจ์ƒ
    int point = 0;
    
    point = score.top(); // stack ์˜ top ์„ ๊บผ๋‚ด์„œ
    score.pop();
        
    score.push(-point);
    return;      
}

void get_point(stack<int> & score, const char region)
{
    int point = score.top();
    score.pop();
    
    if(region == 'S')
    {
        // single
        // do nothing
    }
    else if(region == 'D')
    {
        // double
        point = point*point;
    }
    else if(region == 'T')
    {
        // triple
        point = point*point*point;
    }
    score.push(point);
    point = 0;
    return;
}

int solution(string dartResult) {
    int answer = 0;
    stack<int> score;
    int point = 0;
    bool flag = false;
    
    for(int i = 0 ; i < dartResult.size(); i ++)
    {
        if(isdigit(dartResult[i]) == true)
        {
            point = dartResult[i] - '0';
            
            if(score.top() == 1 && point == 0)
            {
                score.pop();
                score.push(10);
                continue;
            }
            else
            {
                score.push(point);
                continue;
            }       
        }
        else if(dartResult[i] == 'S' || dartResult[i] == 'D' || dartResult[i] == 'T')
        {
            get_point(score, dartResult[i]);
        }
        else if(dartResult[i] == '*')
        {
            star(score);
        }
        else if(dartResult[i] == '#')
        {
            acha(score);
        }
    }
    
    while(score.empty() != true)
    {
        answer += score.top();
        printf("score = %d\n", score.top());
        score.pop();
    }
    
    return answer;
}

 

 

728x90
๋ฐ˜์‘ํ˜•
LIST
Comments