문제
코딩테스트 연습 - [3차] n진수 게임 | 프로그래머스
N진수 게임 튜브가 활동하는 코딩 동아리에서는 전통적으로 해오는 게임이 있다. 이 게임은 여러 사람이 둥글게 앉아서 숫자를 하나씩 차례대로 말하는 게임인데, 규칙은 다음과 같다. 숫자를 0부터 시작해서 차례대로 말한다. 첫 번째 사람은 0, 두 번째 사람은 1, … 열 번째 사람은 9를 말한다. 10 이상의 숫자부터는 한 자리씩 끊어서 말한다. 즉 열한 번째 사람은 10의 첫 자리인 1, 열두 번째 사람은 둘째 자리인 0을 말한다. 이렇게 게임을 진행할
programmers.co.kr
힌트
- 10진수 -> N진수로 변환하는 법
- 예. 10진수 10을 2진수로) 10 / 2 = 5 (나머지 0) -> 5 / 2 = 2 (나머지 1) -> 2 / 2 = 1 (나머지 0) -> 1 / 2 = 0 (나머지 1)
- 뒤로부터 나머지를 나열하면 1010 (십진수 10)
- 예. 10진수 10을 2진수로) 10 / 2 = 5 (나머지 0) -> 5 / 2 = 2 (나머지 1) -> 2 / 2 = 1 (나머지 0) -> 1 / 2 = 0 (나머지 1)
소스 코드
// [프로그래머스] N진수 게임
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
string solution(int n, int t, int m, int p) {
string answer{""};
stack<char> digits;
int turn{1};
int num{0};
int dividend;
int quotient;
int remainder;
int added{0};
bool done{false};
while (1) {
quotient = num;
do {
dividend = quotient / n;
remainder = quotient % n;
if (remainder >= 10) {
digits.push('A' + (remainder - 10));
} else digits.push('0' + remainder);
quotient = dividend;
} while (dividend != 0);
char popChar;
while (!digits.empty()) {
if (turn == p) {
answer.push_back(digits.top());
added++;
if (added == t) return answer;
}
digits.pop();
turn++;
if (turn == (m+1)) turn = 1;
}
num++;
}
return answer;
}
'알고리즘' 카테고리의 다른 글
[프로그래머스] (C++) 숫자 게임 (0) | 2019.12.16 |
---|---|
[프로그래머스] (C++) 구명 보트 (0) | 2019.12.12 |
[프로그래머스] (C++) 가장 큰 수 (0) | 2019.12.10 |
[프로그래머스] (C++) 피보나치 수 (0) | 2019.12.05 |
[프로그래머스] (C++) 행렬의 곱셈 (0) | 2019.12.05 |