Skip to content

Instantly share code, notes, and snippets.

@KOBA789
Created May 1, 2014 17:17
Show Gist options
  • Save KOBA789/805950da7133474b2d89 to your computer and use it in GitHub Desktop.
Save KOBA789/805950da7133474b2d89 to your computer and use it in GitHub Desktop.
課題を解くために作った。
/*
* This program is licensed under the MIT License.
* see: http://koba789.mit-license.org/
*
* node cnv.js 260
*/
var input = Number(process.argv[2] || 0);
var k,
C = [];
for (k = 0; k < 10; ++k) { C.push(k.toString()); }
for (k = 0; k < 26; ++k) { C.push(String.fromCharCode(k + 65)); }
function convert (init, adic) {
var result = [];
var i = init,
q = 0,
m = 0,
r = [];
while (i !== 0) {
q = ~~(i / adic);
m = i % adic;
r.push(C[m]);
result.push(i.toString() + ' / ' + adic.toString() + ' = ' + q.toString() + ' ... ' + C[m]);
i = q;
}
r.reverse();
result.unshift(r.join(''));
return result;
}
function indent (line) { return ' ' + line; }
function format (adic, rows) {
rows[0] = adic.toString() + '進数: ' + rows[0];
rows = rows.map(indent);
rows[0] = rows[0].substring(2);
return rows.join('\n');
}
function answer (input, adic) {
return format(adic, convert(input, adic));
}
console.log(answer(input, 2));
console.log(answer(input, 10));
console.log(answer(input, 16));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment