MOOC清华《程序设计基础》第5章:求n的阶乘(用枚举法做)

来源:互联网 发布:易语言视频解析源码 编辑:程序博客网 时间:2024/06/05 17:58
//使用枚举思想,求解正整数的阶乘//本算法的数学模型为 n!=1*2*...*n #include <iostream>using namespace std;int fact(int n){int m = 1;for(int i = 1; i <= n; i++)m = m * i;return m;}int main(){int n;cin >> n;cout << "fact(" << n << ")=" << fact(n) <<endl;return 0;}


本算法的数学模型为 n! = 1 * 2 * ... * n 。

阶乘的英文全称是factorial 。

阅读全文
0 0
原创粉丝点击