반응형
https://school.programmers.co.kr/learn/courses/30/lessons/17687
풀이
- 우선 10진수를 모든 진법으로 바꿔주는 함수를 작성했다.
string converter(int num, int base) {
string result = "";
string digits = "0123456789ABCDEF";
if (num == 0) return "0";
while (num > 0) {
result = digits[num % base] + result;
num /= base;
}
return result;
}
- 16진법까지이므로, digits는 F까지만 작성했다. 진법이 늘어나면 뒤로 더 추가하면 된다.
전체 코드
#include <string>
#include <vector>
using namespace std;
string converter(int num, int base) {
string result = "";
string digits = "0123456789ABCDEF";
if (num == 0) return "0";
while (num > 0) {
result = digits[num % base] + result;
num /= base;
}
return result;
}
string solution(int n, int t, int m, int p) { // 진법, 구할 개수, 참가 인원, 자기 순서
string answer = "";
string word = "";
int cnt = 0;
while(word.size() < t * m) {
word += converter(cnt, n);
cnt++;
}
for(int i=0; i<t; i++) {
answer += word[p - 1];
p += m;
}
return answer;
}
- 참가 인원 * 구할 개수만큼은 문자열이 생기도록 구해놓고
- 자기 순서에 맞게 문자를 뽑아냈다.
반응형
'코딩테스트' 카테고리의 다른 글
[c++] 백준 - 17396_백도어 (0) | 2024.08.15 |
---|---|
[c++] 백준 - 5972번_택배 배송 (0) | 2024.08.15 |
[c++] 프로그래머스 - 프렌즈4블록 (0) | 2024.08.15 |
[C++] 프로그래머스 - 이중우선순위큐 (0) | 2024.04.15 |
[C++] 프로그래머스 - 기능개발 (0) | 2024.04.15 |