プログラマー(Java)-[3回目]n進数ゲーム
9466 ワード
質問リンク
https://programmers.co.kr/learn/courses/30/lessons/17687
問題を解く
11~16進数では、2つの数字をA、B、C、D、E、Fで表す.
changeNum()メソッドによる
入力した数値に対して、対応する進数で表される数値を返します.
残りの処理は最低の位置から開始するのでスタックに入れて取り出し、最高の桁数から開始する.
コード#コード#
import java.util.*;
class Solution {
public String solution(int n, int t, int m, int p) {
String answer = "";
//숫자는 0부터 시작
int cnt =0;
String str = "";
//str의 길이는 t*m
while(str.length()<t*m){
String temp = changeNum(cnt, n);
str+=temp;
cnt++;
}
//System.out.println(str);
int idx = p-1;
while(true){
answer+=str.substring(idx,idx+1);
idx +=m;
if(answer.length()==t){
break;
}
}
return answer;
}
public String changeNum(int cnt, int n){
String dictionary = "ABCDEF";
if(cnt==0){
return "0";
}
Stack<String> st = new Stack();
while(cnt>0){
int res =cnt%n;
if(n>=10 && 10<=res && res<n){
st.push(dictionary.substring(res-10, res-10+1));
}else{
st.push(String.valueOf(res));
}
cnt = cnt/n;
}
String result = "";
while(!st.isEmpty()){
result+=st.pop();
}
return result;
}
}
Reference
この問題について(プログラマー(Java)-[3回目]n進数ゲーム), 我々は、より多くの情報をここで見つけました https://velog.io/@courage331/프로그래머스Java-3차-n진수-게임テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol