[b]程序如下:
#include<iostream>
using namespace std;
int main()
{
void convert(int n);
int number;
cout<<"input an integer:";
cin>>number;
cout<<"output:"<<endl;
if(number<0)
{
cout<<"-";
number=-number;
}
convert(number);
cout<<endl;
return 0;
}
void convert(int n)
{
int i;
char c;
if((i=n/10)!=0)
convert(i);
c=n%10+'0';
cout<<" "<<c;
}
结果是:如果输入的是345,则输出时 3 4 5
本人认为输出结果应该是 3
为什么会是 3 4 5 呢?
求好心人帮忙解答下!

解决方案 »

  1.   

    在递归的时候cout<<" "<<c输出了三次
    第一次你n = 3时候 n = 34时候 n =345的时候!
    不是最后一次等于3的时候就返回了....,还需要回溯到上一层
      

  2.   

    if((i=n/10)!=0) convert(i);
    else
    {// 最后1次
       c=n%10+'0';
       cout<<" "<<c;
    }
      

  3.   

    main调用convert(345),convert(345)又调用 convert(34),convert(34)又调用convert(3),
    convert(3)输出 3, 然后回到convert(34),输出 4, 再回到 convert(345), 输出 5.