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

View File

@@ -63,6 +63,7 @@ int main() {
for (i = 0; i < 40; i++) {
people[i] = ALIVE;
}
startTime = clock();
printf("Stand at %d\n",recursion(0,0)+1);
finishTime = clock();

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;
}

29
08/Optional01.c Normal file
View File

@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdbool.h>
bool checkDigits(int chicken, int rabbit) {
bool digits[6] = {0};
int num = chicken*1000 + rabbit;
while (num > 0) {
if (num % 10 > 6 || digits[num % 10]) {
return false;
}
else {
digits[num % 10] = true;
num /= 10;
}
}
return true;
}
int main() {
int count = 50;
for (count = 50; count < 250; count++) {
if (checkDigits(count * 2, count * 4)) {
printf("There are %d chickens and rabbits. %d chicken legs, %d rabbit legs.", count, count*2, count*4);
}
}
return 0;
}

36
08/Optional02.c Normal file
View File

@@ -0,0 +1,36 @@
#include <stdio.h>
int sumSquares(int num) {
if (num == 1) {
return 1;
}
else {
return num*num + sumSquares(num - 1);
}
}
int sumSum(int k) {
if (k == 1) {
return 1;
}
else {
return k*sumSquares(k) + sumSum(k - 1);
}
}
int main() {
int S, NS;
int n = 50;
S = sumSum(n);
NS = n*(n+1)/2*(n+2)/3*(8*n*n+11*n + 1)/20;
if (S > NS) {
printf("1");
}
else if (S == NS) {
printf("0");
}
else {
printf("-1");
}
return 0;
}

15
08/test.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include <stdio.h>
#ifndef __x86_64__
#define __x86_64__ 0
#endif
int main() {
printf("x86_64: %d\n", __x86_64__);
printf("arm64: %d\n", __arm64__);
printf("macOS: %d\n", __MAC_13_0);
int a=0;
printf("%d", 5/a);
return 0;
}