用的vs20134编辑的,为什么输入20得不到正确答案???
#include<stdio.h>int hanshu(int n);
int main()
{
int n;
int result;
scanf("%d", &n);
result = hanshu(n);
printf("%d", result);
}
int hanshu(int n)
{
int linshijieguo;
if (n == 0 || n == 1)
{
linshijieguo = 1;
}
else if (n < 0)
{
printf("n<0,input error!\n");
}
else
{
linshijieguo = hanshu(n - 1)*n;
}
return linshijieguo;
}
我觉得这样更好!
#include<iostream>using namespace std;
double hanshu(int n);
int main()
{
int n;
double result;
cin >> n;
result = hanshu(n);
cout << result;
}
double hanshu(int n)
{
if (n > 1)
return hanshu(n - 1)*n;
return 1;
}
页:
[1]