// 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;
else
f = 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;
}
寫一個方法,輸入任意一個整數,返回它的階乘
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.