对于给定的十进制正整数,打印出对应的八进制正整数。(1):写出递归算法(2):写出非递归算法,用栈实

用数据结构编写
2024-12-15 12:28:39
推荐回答(1个)
回答1:

没有测试过,有问题来http://www.douban.com/note/66682462/ 回复

(1)递归算法

long M-to-X(M-number)
{
/*
M mod 8 , output their remainders in reverse order
M-number is the individor
*/
long r = 0; //remainder
long R = 0; //result

if (M-number < 8)
{
return M-number;
}
else
{
r = M-number % 8;
R = M-number / 8;
return r + M-to-X(R) * 10;
}
}

(2)非递归算法,用栈实

long M-to-X(M-number)
{
/*
M mod 8 , output their remainders in reverse order
M-number is the individor
*/
long r = 0; //remainder
long R = 0; //result
long OX = 0; //OX-number

do{
r = M-number % 8;
R = M-number / 8;
push(s, r);

}while (R > 8);

while(!isEmpty(s)) /*s is a stack*/
{
OX = OX * 10 + pop(s);
}
printf("M-number = [%ld], is to OX-number = [%ld]\n", M-number, OX);
return OX;
}