Lesson 8.

This commit is contained in:
unlockable
2022-11-07 13:49:28 +08:00
parent d12429be97
commit 777e33d692
5 changed files with 124 additions and 0 deletions

43
08/Exercise02.c Normal file
View File

@@ -0,0 +1,43 @@
#include <stdio.h>
#include <time.h>
int goDown(int n) {
if (n == 1) {
return 1;
}
else if (n == 2) {
return 2;
}
else if (n == 3) {
return 4;
}
else {
return goDown(n-1) + goDown(n-2) + goDown(n-3);
}
}
int main() {
int n = 0;
int i = 0, j = 0;
int startTime, finishTime;
double time;
double timeSum;
int ans;
for (i = 5; i < 21; i++) {
printf("%d: %d\n", i, goDown(i));
}
for (j = 15; j <=35; j+=10) {
printf("n = %d\n", j);
for (i = 0; i < 3; i++) {
startTime = clock();
ans = goDown(j);
finishTime = clock();
time = ((double)finishTime - startTime) / CLOCKS_PER_SEC;
timeSum += time;
printf("%.16lf\n", time);
}
printf("Avg: %lf\n", timeSum/3);
timeSum = 0;
}
return 0;
}