约瑟夫环~
int s = 0;
for (i = 2; i <= n; i++)
s = (s + t) % i;
printf("%d", s+1);
虽然很短 但还是第一次这么自信的打这几行代码
[ 本帖最后由 BlueGuy 于 2009-8-13 15:12 编辑 ]
#include<stdio.h> #include<malloc.h> #include<stdlib.h> struct M { int num; struct M *next; }; int main(void) { int i, j, n, t; struct M *p, *q, *head; p = (struct M*)malloc(sizeof(struct M)); printf("The number of monkeys: "); scanf("%d", &n); printf("The amount of count: "); scanf("%d", &t); for(i = 1;i <= n;i++) { q = (struct M*)malloc(sizeof(struct M)); q->num = i; if(i == 1) head = q; p->next = q; p = q; } p->next = head; // p = head;你的主要问题出现在这 for(i = 0;i <= n-2;i++) { for(j = 0;j <= t-1;j++) { q = p; p = p->next; } q->next = p->next; free(p); p = q; } printf("The king: %d\n", p->num); return 0; }