n진법으로 한글자 씩 말하기 게임에서  튜브의 순서에서 말해야하는 숫자를 정답으로 내면된다.

1. 튜브의  순서에서 말해야할 숫자는 p-1 번째  부터 m개를 더해가며 반복해주면된다.

2. n진법을 총 문자열에 더해주는데 이때 중간에 stack에 push 하고 top을 pop하는 과정에서 숫자에서 10이 넘는 경우 A~F로 변환이 가능하도록 한다. 

3. 사람들이 말하는 숫자의 최대 개수는 t*m 이다.

#include <string>
#include <vector>
#include <stack>

using namespace std;

string makestr(int n,int max){          //n진수, max는 길이 최대값 => t*m 
    string str="";
    stack<int> s;               //중간에 수를 저장할 스택변수
    
    for(int i=0;str.length()<=max;i++){
        int x=i;
        
        while(1){
            s.push(x%n);
            x/=n;
            
            if(x==0) break;
        }
        
        while(!s.empty()){
            if(s.top()<10){
                str+=to_string(s.top());
            }else{
                str+=(char)(65+s.top()-10);     //10보다 크다면 A~F로 변환
            }
            s.pop();
        }
    }
    
    return str;
    
}

string solution(int n, int t, int m, int p) {
    string answer = "";
    
    string str=makestr(n,m*t);
    
    for(int i=p-1;t>0;i+=m){
        answer+=str[i];
        t--;
    }
    return answer;
}

+ Recent posts