|
>10進数をn進数にかえる関数のプログラムをおしえてくさい
ここは、C言語の掲示板ですよ。なぜ「C++で」なんですか。おしえて「くさい」 でも、おもしろいから、解のひとつを示しましょう。
#include <iostream>
#include <string>
using namespace std;
string decimal_to_base_n(unsigned int x, unsigned int n)
{
string s;
if (x >= n) s = decimal_to_base_n(x / n, n);
return s + "0123456789abcdefghijklmnopqrstuvwxyz"[x % n];
}
int main()
{
unsigned int x, n;
while (cin >> x >> n && n >= 2 && n <= 36)
cout << decimal_to_base_n(x, n) << endl;
return 0;
}
|