修改文件结构。

This commit is contained in:
unlockable
2023-02-21 10:56:54 +08:00
parent a64cfdd9f3
commit 40182871f4
83 changed files with 0 additions and 0 deletions

73
POP/08/Exercise01.c Normal file
View File

@@ -0,0 +1,73 @@
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#define DEAD true
#define ALIVE false
bool people[40] = {0};
void iteration() {
int nowCount = 0, nowPos = 0, nowDead = 0;
while (nowDead < 39) {
nowCount = 0;
while (nowCount < 3) {
if (people[nowPos % 40]==ALIVE) {
nowCount++;
}
nowPos++;
}
people[(nowPos+39)%40] = DEAD;
nowDead+=1;
}
for (nowPos = 0; nowPos < 40; nowPos++) {
if (people[nowPos % 40] == ALIVE) {
printf("Stand at %d\n", nowPos+1);
break;
}
}
}
int recursion(int nowDead, int nowPos) {
int nowCount=0;
if (nowDead == 39) {
for (nowPos = 0; nowPos < 40; nowPos++) {
if (people[nowPos % 40] == ALIVE) {
// printf("Stand at %d\n", nowPos+1);
return nowPos;
}
}
}
else {
while (nowCount < 3) {
if (people[nowPos % 40]==ALIVE) {
nowCount++;
}
nowPos++;
}
people[(nowPos+39)%40] = DEAD;
return recursion(nowDead+1, nowPos);
}
}
int main() {
int startTime, finishTime;
double time;
int i = 0;
startTime = clock();
iteration();
finishTime = clock();
time = ((double)(finishTime - startTime))/CLOCKS_PER_SEC;
printf("Time elapsed: %lfs\n", time);
for (i = 0; i < 40; i++) {
people[i] = ALIVE;
}
startTime = clock();
printf("Stand at %d\n",recursion(0,0)+1);
finishTime = clock();
time = ((double)(finishTime - startTime))/CLOCKS_PER_SEC;
printf("Time elapsed: %lfs", time);
return 0;
}