// factorial.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <CSTDIO>
#include <CSTDLIB>
#include <IOSTREAM>
using namespace std;
// 1、循环
long fact(int n)
{
int i;
long result = 1;
for (i = 1; i <= n; i++)
{
result *= i;
}
return result;
}
// 2、递归
// n! = n * (n - 1)!
long fact1(int n)
{
if (n <= 1)
{
return 1;
}
else
{
return n * fact1(n-1);
}
}
int main(int argc, char* argv[])
{
int i;
cout<<"enter factorial number: "<<endl;
cin>>i;
cout<<i<<" factorial: "<<fact(i)<<endl;
cout<<i<<" factorial recursion: "<<fact1(i)<<endl;
return 0;
}
阶乘算法
最新推荐文章于 2021-10-03 11:33:22 发布
1886

被折叠的 条评论
为什么被折叠?



