10์ง๋ฒ ์๋ฅผ n์ง๋ฒ์ ๋ง๋ ๋ฌธ์์ด๋ก ๋ฐํํ๋ convert() ๋ฉ์๋๋ฅผ ์ ์ํ์๊ณ
0๋ถํฐ n์ง๋ฒ์ผ๋ก ๋ณํํ ๋ฌธ์์ด์ ๊ณ์ ๋ํด์ ๊ธธ์ด๊ฐ t*m ์ด์์ด ๋ ๋๊น์ง ๋ํ๋ค.
๊ฒ์์ ์ฐธ๊ฐํ๋ ์ธ์์ด m๋ช ์ด๊ณ , ๋ฏธ๋ฆฌ ๊ตฌํ ์ซ์๊ฐ t์ผ๋, ๋ง์ฝ ํ๋ธ์ ์์๊ฐ m๋ช ์ค์ m๋ฒ์งธ ์์์ธ ๊ฒฝ์ฐ๊ฐ ์ ์ผ ๋ฆ์ ์์์ธ ๊ฒฝ์ฐ์ด๋ฏ๋ก ๋ฌธ์์ด์ t*m๊น์ง๋ง ๊ตฌํ๋ฉด ๋๊ธฐ ๋๋ฌธ์ด๋ค.
๊ทธ๋ฆฌ๊ณ do-while ๋ฃจํ๋ฌธ์ผ๋ก ์ฃผ์ด์ง ํ๋ธ์ ์์์ ๋ง๊ฒ t๊ฐ๊ฐ ๋ ๋๊น์ง ๋ฌธ์์ด์ ๋ํด์ ๋ต์ ๊ตฌํ๋ค.
// ์ง๋ฒ n
class Solution {
String[] unit = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
public String solution(int n, int t, int m, int p) {
String result = "";
int i = 0;
while(result.length()<=t*m){
result+=convert(i++,n);
}
String answer = "";
int idx=p-1;
do{
answer+=result.charAt(idx);
idx+=m;
}while(answer.length()!=t);
return answer;
}
String convert(int num, int n){
StringBuilder sb = new StringBuilder();
if(num==0){
return "0";
}
while(num!=0){
sb.append(unit[num%n]);
num/=n;
}
return sb.reverse().toString();
}
}