我正在編寫一個程序來顯示計算給定數字階乘200萬次所需的時間。我在C/C++ Eclipse環境中使用Debian Linux編寫它。當程序到達int temp = n * rfact(n-1);
時,它掛起,不會做任何事情。C中的遞歸階乘程序在執行時掛起
這裏是我到目前爲止有:
#include <stdio.h>
#include <time.h>
//prototypes
int rfact(int n);
main()
{
int n = 0;
int i = 0;
double result = 0.0;
clock_t t;
printf("Enter a value for n: ");
scanf("%i", &n);
printf("n=%i\n", n);
//get current time
t = clock();
//process factorial 2 million times
for(i=0; i<2000000; i++)
{
rfact(n);
}
printf("n=%i\n", n);
//get total time spent in the loop
result = (clock() - t)/(double)CLOCKS_PER_SEC;
//print result
printf("runtime=%d\n", result);
}
//factorial calculation
int rfact(int n)
{
int temp = n * rfact(n-1);
printf(i++);
return temp;
}
哦哇大facepalm那裏有一個好點 – zakparks31191 2013-02-25 01:19:58