用递归求阶乘n!
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
// 求阶乘2参考他人.cpp: 主项目文件。
#include "stdafx.h"
using namespace System;
#include <iostream>
using namespace std;
//计算阶乘的函数
long CalcJiecheng(int num)
{
int res = 0;
if (1== num) //边界条件
{
res = 1;
}
if (num>1) //递归公式
{
res = num*CalcJiecheng(num-1);
}
return res;
}
int main(array<System::String ^> ^args)
{
int num;//定义
cin>>num;
long res = CalcJiecheng(num);//定义变量储存阶乘
cout<<num<<"的阶乘:"<<res<<endl;
system("pause");
return 0;
}