对于给定的十进制正整数,打印出对应的八进制正整数。(1):写出递归算法(2):写出非递归算法,用栈实
收起
知与谁同
2018-07-17 18:26:04
2163
0
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;
}
2019-07-17 22:55:13