修改文件结构。
This commit is contained in:
73
POP/08/Exercise01.c
Normal file
73
POP/08/Exercise01.c
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user