C++_静态局部变量求阶乘

来源:互联网 发布:龙江网络客服中心电话 编辑:程序博客网 时间:2024/06/10 02:35
/*******************************时间:2012年10月17日11:09:02 **描述:静态局部变量,求阶乘   *******************************/# include <iostream>using namespace std;int fac(int);//函数声明int main(){int i;for(i = 1; i <= 5; i++){cout<<i<<"! = "<<fac(i)<<endl;}return 0;}int fac(int n){static int f = 1;//f为静态局部变量,函数结束时f的值不释放f = f * n;//在f原值基础上乘以nreturn f;}/*****************************  在vc++6.0中运行的结果是:**  -----------------------  **  1! = 1                   **  2! = 2                   **  3! = 6                   **  4! = 24                  **  5! = 120                 **  -----------------------  *****************************/

原创粉丝点击