70,034
社区成员
发帖
与我相关
我的任务
分享#include <stdio.h>
long Power_loop (int m,int n)
{
long j;
for (n = 1;n <= 3;n++){
j = m * n;
}
return j;
}
long PowerRecursive (int a,long b)
{
if (a < 0)return -1;
return PowerRecursive(a,b-1) * a;
}
int main(void)
{
printf("loop = %ld\n",Power_loop(2,3));
printf("Recursive = %ld\n",PowerRecursive(2,3));
return 0;
}#include <stdio.h>
long Power_loop (int m,int n)
{
long result = 1;
for (int i = 1; i <= n;i++){
result *= m;
}
return result;
}
long PowerRecursive (int a,int b)
{
if (b > 1)//改这里
return PowerRecursive(a,b-1) * a;
else
return a;//改这里
}
int main(void)
{
printf("loop = %ld\n",Power_loop(9,8));
printf("Recursive = %ld\n",PowerRecursive(9,8));
return 0;
}
如果是求阶乘的话,就该这么写啊
#include<stdio.h>
int f(int n)
{
if(n == 1 || n == 2)
return n;
else
return f(n-1)*n;
}
int main()
{
int n;
scanf("%d",&n);
printf("%d",f(5));
return 0;
}#include <stdio.h>
long Power_loop (int m,int n)
{
long result = 1;
for (int i = 1; i <= n;i++){
result *= m;
}
return result;
}
long PowerRecursive (int a,int b)
{
if (b > 0)
return PowerRecursive(a,b-1) * a;
else
return 1;
}
int main(void)
{
printf("loop = %ld\n",Power_loop(9,8));
printf("Recursive = %ld\n",PowerRecursive(9,8));
return 0;
}