利用函数递归计算阶乘
递归: 调用函数本身。
下面例子是简单的计算阶乘算法。注:数据只限制在int 范围,如需更大范围的计算,请自行改变数据的类型即可。
1 #include<iostream> 2 #include <iomanip> 3 #include <string> 4 #include <stdio.h> 5 #include <time.h> 6 #include <windows.h> 7 using namespace std; 8 9 int fun(int x) 10 { 11 int t; 12 if (x == 0 || x==1) 13 { 14 return 1; 15 } 16 else 17 { 18 t = x*fun(x-1); 19 return t; 20 } 21 } 22 23 int main(void) 24 { 25 int y; 26 y = fun(5); 27 cout << "y!=" << y << endl; 28 system("PAUSE"); 29 return 0; 30 }
thanks.