코딩테스트/문자열
[문자열] 프로그래머스 [3차] n진수 게임 C++
유(YOO)
2022. 2. 4. 10:40
https://programmers.co.kr/learn/courses/30/lessons/17687
코딩테스트 연습 - [3차] n진수 게임
N진수 게임 튜브가 활동하는 코딩 동아리에서는 전통적으로 해오는 게임이 있다. 이 게임은 여러 사람이 둥글게 앉아서 숫자를 하나씩 차례대로 말하는 게임인데, 규칙은 다음과 같다. 숫자를 0
programmers.co.kr
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
string solution(int n, int t, int m, int p) {
char ch[16]={'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; // 저장해서 사용하기
string answer = "";
string str = "";
int num = 0;
while(str.length() < t*m) {
string temp = "";
int divider = num;
while(divider/n!=0) {
temp += ch[divider%n];
divider /= n;
}
temp+=ch[divider%n];
reverse(temp.begin(), temp.end());
str += temp;
cout << temp << " " ;
num++;
}
for(int i=p-1; ; i+=m) {
answer += str[i];
if(answer.length() == t) break;
}
return answer;
}