我尝试了以下代码将一个十进制数转换为其他进制。如果目标进制中没有零(0),则代码可以正常工作。比如79和3,输出的结果是正确的:2221。 但当我将19和3进行转换时,结果应该是201而不是21,这表明出现了错误。
int x, y, a = 0, i, t, j;
cout << "enter two numbers" << endl;
cin >> x >> y; // x as the number in base-10 and x, as the destination base
a = x;
while (x >= y)
{
t = 1;
for (i = 0; x > y; i++)
{
x /= y;
}
cout << x;
for (j = 0; j < i; j++)
{
t *= y;
}
a = a - (t*x);
x = a;
}
cout << x<<endl;
3 3
,你会看到另一个错误。 - genisage