C++问题(用递归调用法编程)

来源:百度知道 编辑:UC知道 时间:2024/07/05 12:46:52
大家帮帮忙,我不熟递归,帮忙编一道题!
用递归调用法把一个正整数转换成字符串输出,例如,输入整数523479,输出字符串“523479“,正整数位数不得超过10位!

#include<iostream.h>

void f(int a)
{
cout<<char(a%10+'0');//正整数转换成字符串
if((a/=10)!=0)
f(a);
}
void main()
{
int n;
cin>>n;
f(n);
cout<<'\n';
}

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

string convert(int n);

void main()
{
int nNum;
string str="";
cin>>nNum;

if(nNum<0)
{
str=str+'-';
nNum=-nNum;
}
str=str+convert(nNum);

cout<<str<<endl;
}

string convert(int n)
{
static string c="";
if(n<10)
{
c=(char)(n%10+'0');
return c;
}
else
{
c=convert(n/10)+(char)(n%10+'0');
return c;
}
}

你的意思是把那个数的各位递归分解吧?
void fj(long int&digit)