CCF201712-2 遊戲(100分)C語言
阿新 • • 發佈:2019-01-01
#include <stdio.h>
//環形迴圈佇列實現,陣列資料為0代表小朋友淘汰,不為0加1則代表小朋友編號
int main(){
int a[1000] = {0};
int n, k = 0;
scanf("%d",&n);
scanf("%d",&k);
//num代表每次的報數,front,rear分別為佇列頭和佇列尾
int num = 0, i = 0, front = 0, temp = 0;
int rear = n - 1;
//小朋友賦值對應編號
for(i = 0; i < n; i++) a[i] = i+1;
while(front != rear){
num++;
if(( num % k) == 0 || ( num % 10) == k) {
a[front] = 0;
front = (front + 1) % n;
}
else{
//佇列頭的元素插入佇列尾後刪除
temp = a[front];
a[front] = 0;
front = (front + 1) % n;
rear = (rear + 1) % n;
a[rear] = temp;
}
}
printf("%d",a[front]);
return 0;
}