[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 呢?
求好心人帮忙解答下!
#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 呢?
求好心人帮忙解答下!
第一次你n = 3时候 n = 34时候 n =345的时候!
不是最后一次等于3的时候就返回了....,还需要回溯到上一层
else
{// 最后1次
c=n%10+'0';
cout<<" "<<c;
}
convert(3)输出 3, 然后回到convert(34),输出 4, 再回到 convert(345), 输出 5.