一定要用嵌套吗?
double power(double x,int n)
{
int a,sum=1;
for(a=0;a<=n;a++)
sum = x;
printf("%f",x);
}
或者
double power(double x,int n)
{
if(n==0)
return 1;
else
return xpower(x,n-1):
}
#include<stdioh>
int power(int x,int n)
{
if(n==0)
return 1;
elseif(n%2==1)
return xpower(x,n-1);
else{
int y=power(x,n/2);
return yy;
}
}
int main()
{
int a,b,c;
printf("enter x and n:");
setvbuf(stdout,NULL,_IONBF,0);
scanf("%d%d",&a,&b);
c=power(a,b);
printf("结果为%d",c);
return 0;
}
扩展资料
#include<stdioh>
double power(double x,int n);
main()
{
double x;
int n;
printf("Input x,n:");
scanf("%lf,%d",&x,&n);
printf("%2lf",power(x,n));
}
double power(double x,int n)
{
double a=10;
int i;
for(i=1;i<=n;i++)
a=x;
return a;
}
参考资料:
#include <stdioh>
float xn(int x,int n)
{
int i;
float s=1;
for(i=1;i<=n;i++)
{
s=sx;
}
return s;
}
int main()
{
int x,n;
float result;
printf("please input x:");
scanf("%d",&x);
printf("please input n:");
scanf("%d",&n);
result=xn(x,n);
printf("x^n=%0f\n",result);
return 0;
}
程序已经调试过了,你看下吧,可以的话给个好评
#include"iostream"
using namespace std;
float power(float a,int n)
{
if (n==1)return a;
else return a=power(a,n-1);
}
int main()
{
float a;
int n;
cout<<"请输入实数a和正整数n:"<<endl;
cin>>a>>n;
float i=power(a,n);
cout<<"n次方后:"<<"a="<<i<<endl;
cout<<"n="<<n<<endl;
system("pause");
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)