KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • tree
  • js
  • Level 1
  • Level 2
  • Level 0
  • 다이나믹 프로그래밍
  • string
  • codeup
  • recursion
  • Level1
  • 백준
  • LeetCode
  • C++
  • programmers
  • 그래프
  • 문자열
  • C
  • 정렬
  • 수학
  • 제코베 JS 100제

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

Programmers / Level 1 / [1차] 다트 게임 / C++ / JS
PS/Programmers

Programmers / Level 1 / [1차] 다트 게임 / C++ / JS

2021. 11. 22. 21:46

< 문제 바로가기 >

 

프로그래머스

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

programmers.co.kr

 

< C++ >

#include <string>
#include <cmath>

using namespace std;

int solution(string dartResult) {
    int answer = 0;
    int score = 0, prev = 0;
    
    for(int i=0; i<dartResult.length(); i++) {
        char tmp = dartResult[i];
        
        if(tmp >= '0' && tmp <= '9') {
            prev = score;
            
            if(dartResult[i+1] == '0') {
                score = 10;
                i++;
            }
            else {
                score = tmp - '0';
            }
        }
        
        else if(tmp == 'S' || tmp == 'D' || tmp == 'T') {
            if(tmp == 'D') {
                score = pow(score, 2);
            }
            if(tmp == 'T') {
                score = pow(score, 3);
            }
            
            if(dartResult[i+1] == '*') {
                // prev 2배
                // answer에 이미 prev가 더해져있기 때문에 한번만 더해주면 됨
                answer += prev;
                score *= 2;
                i++;
            }
            
            if(dartResult[i+1] == '#') {
                score = -score;
                i++;
            }
            answer += score;
        }
    }
    
    return answer;
}

< JS >

function solution(dartResult) {
  const BONUS = { S: 1, D: 2, T: 3 };
  const OPTION = { '#': -1, '*': 2 };
  let [prevScore, score] = [0, 0];
  let answer = 0;

  for (let i = 0; i < dartResult.length; i += 1) {
    const DART = dartResult[i];

    if ('0' <= DART && DART <= '9') {
      prevScore = score;
      // 점수가 10점일때
      if (dartResult[i + 1] === '0') {
        score = 10;
        i += 1;
      } 
      else score = parseInt(DART);
    } 
    
    // bonus
    else if (DART === 'S' || DART === 'D' || DART === 'T') {
      score = score ** BONUS[DART];

      // option
      if(dartResult[i + 1] === '*' || dartResult[i + 1] === '#') {
        if (dartResult[i + 1] === '*') {
          // 이미 answer에 더해져있기 때문에 한번만 더함
          answer += prevScore;
        }
        score *= OPTION[dartResult[i + 1]];
        i += 1;
      }

      answer += score;
    }
  }

  return answer;
}
저작자표시

'PS > Programmers' 카테고리의 다른 글

Programmers / Level 2 / 가장 큰 수 / C++  (0) 2022.01.07
Programmers / Level 1 / [1차] 비밀지도 / C++  (0) 2022.01.06
Programmers / Level 1 / 이상한 문자 만들기 / C++  (0) 2021.11.20
Programmers / Level 1 / 최소직사각형 / C++  (0) 2021.11.13
    'PS/Programmers' 카테고리의 다른 글
    • Programmers / Level 2 / 가장 큰 수 / C++
    • Programmers / Level 1 / [1차] 비밀지도 / C++
    • Programmers / Level 1 / 이상한 문자 만들기 / C++
    • Programmers / Level 1 / 최소직사각형 / C++
    KimMinJun
    KimMinJun

    티스토리툴바