programmers

[c++ programmers N진수 변환]

vicky_ 2022. 3. 8. 14:24
728x90
#include <string>
#include <vector>
#include <math.h>
using namespace std;

vector<char> res;

void conv(int num, int b){
	if (num == 0){
        res.push_back(48);
    }
    
    while (num > 0){
        if (num % b < 10){
            res.push_back(num % b + 48);
        }
        else{
            res.push_back(num % b + 55);
        }
        num /= b;
    }
}

string solution(int n, int t, int m, int p) {
    string answer = "";
    int start = 1;	// 숫자 세기
    int count = 1;	// 자리 수 세기
    
    while (answer.size() < t){
        // n진수 변환
        conv(start, n);

        // 변환 한 수가 순서에 부합하면 answer에 추가하기 
        for (int i = res.size() - 1; i >= 0; --i){
            if (count <= m){
                if (count == p){
                    cout << start << endl;
                    answer += res.at(i);
                }
            }
            else{
                int temp = count;
                while (temp > m){
                    temp -= m;
                }
                if (temp == p){
                    answer += res.at(i);
                }
            }
            ++count; // 자릿수 증가
            if (answer.size() == t){
                break;
            }
        }
        res.clear();
        ++start; // 숫자 증가
    }

    return answer;
}