c语言编程:用递归与非递归求n!
1个回答
展开全部
由于长整型数据有限制 这个阶层算不了多大的 测试的时候就用一位数吧
如果不能用再追问我吧
①递归
#include "stdio.h"
#include "conio.h"
main()
{
int input;
long result;
long f(int n);
scanf("%d",&input);
result = f(input);
printf("%d! = %ld",input,result);
}
long f(int n)
{
if(n<0){
printf("函数输入无效!");
return;
}
if(n==0)
{
return (long)1;
}
if(n>0)
{
return (long)n*f(n-1);
}
}
②非递归
#include "stdio.h"
#include "conio.h"
main()
{
int input,i;
long result = 1;
do{
printf("Please input an integer that is bigger than 0!\n");
scanf("%d",&input);
}while(input<0);
for(i=input;i>1;i--)
{
result = result*i;
}
printf("%d! = %ld",input,result);
}
如果不能用再追问我吧
①递归
#include "stdio.h"
#include "conio.h"
main()
{
int input;
long result;
long f(int n);
scanf("%d",&input);
result = f(input);
printf("%d! = %ld",input,result);
}
long f(int n)
{
if(n<0){
printf("函数输入无效!");
return;
}
if(n==0)
{
return (long)1;
}
if(n>0)
{
return (long)n*f(n-1);
}
}
②非递归
#include "stdio.h"
#include "conio.h"
main()
{
int input,i;
long result = 1;
do{
printf("Please input an integer that is bigger than 0!\n");
scanf("%d",&input);
}while(input<0);
for(i=input;i>1;i--)
{
result = result*i;
}
printf("%d! = %ld",input,result);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询