写一个方法,输入任意一个整数,返回它的阶乘

来源:互联网 发布:mvc4后台开发框架源码 编辑:程序博客网 时间:2024/05/29 07:04
// jiecheng.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>//递归实现long fac(int n){long f;if(n == 0)f = 1;elsef = n*fac(n-1);return f;}//非递归实现long fac_2(int n){int t,result = 1;for(t = 1; t <= n; t++){result *= t;}return result;}int _tmain(int argc, _TCHAR* argv[]){long y;int n;scanf("%d",&n);y = fac(n);printf("%d!=%ld",n,y);y = fac_2(n);printf("\n");printf("%d!=%ld\n",n,y);return 0;}

原创粉丝点击