문제
다음 큰 숫자
문제풀이
var m = [];
function convertBinary (num) {
var binary = [];
while (num != 0) {
var quote = num % 2;
num = parseInt(num / 2);
binary.push(quote);
}
return binary;
}
function convertRecursive (strNum, cnt) {
var nums = [];
var sCnt = 0;
strNum++;
m = convertBinary(strNum);
for (var i = 0; i < m.length; i++) {
if(m[i] == 1) sCnt++;
}
if (sCnt != cnt) {
m = [];
convertRecursive(strNum, cnt);
} else {
return;
}
}
function solution(n) {
var answer = 0;
var cnt = 0;
var strNum = convertBinary(n);
for (var i = 0; i < strNum.length; i++) {
if(strNum[i] == 1) cnt++;
}
convertRecursive(n, cnt);
m = m.reverse();
var binary = m.join('');
var decimal = parseInt(binary, 2);
return decimal;
}