请教一个C++递归问题

来源:百度知道 编辑:UC知道 时间:2024/06/04 21:00:04
编写一个程序,要求用户输入一个数和一个指数,编写一个递归函数,接受这两个数做为参数,并计算幕运算结果。也就是说 如果数是2 指数是4 该函数返回16;如果是3 指数4 返回81。
#include <iostream>
int ch(int x,int y);
using namespace std;

int main ()
{
int x,y,z;
cin>>x;
cin>>y;
z=ch(x,y);
cout<<"z="<<z;
char response;
cin>>response;
return 0;
}
int ch(int x,int y)
{
if (y==1)
return x;
else
{

return x*ch(x,y--);
}
}
这是我自己写的 但是运行了得不到想要的结果

#include <iostream>
int ch(int x,int y);
using namespace std;

int main ()
{
int x,y,z;
cin>>x;
cin>>y;
z=ch(x,y);
cout<<"z="<<z;
char response;
cin>>response;
return 0;
}
int ch(int x,int y)
{
if (y==1)
return x;
else
{
return (x*ch(x,y-1));
}
}

#include <iostream>

using namespace std;

long ch (long a,long b)
{
if (b=0) return 1;
else if (b=1) return a;
else return a*(ch(a,b--));
}

int main ()
{
long x,y,z;
cin>>x>>y;
z=ch(x,y);
cout<<z<<endl;
system ("pause");
return 0;
}

#include<iostream>
using namespace std;

void main()
{
int i,j; //定义底数和指数
cout<<"请输入底数i(i!=0)和指数j(j>=0):";