zte面试题

题:15个人依次从1-15编号,然后围成圈,1、2、3循环报数,报3者退出,问最后剩下的是编号为多少的人?

 

好像计算机三级的习题上出现过, 哈哈...

#include <stdio.h> #include <stdlib.h> #define TOTAL 15 #define LOOP 3 typedef struct _Person{ int num; struct _Person *next; struct _Person *preview; }Person; int main() { int i; Person *temp, *p, *h, *q; h = p = q =(Person *)malloc(sizeof(Person)); for (i = 1; i <= TOTAL; i++) { temp = (Person *)malloc(sizeof(Person)); temp->num = i; p->next = temp; if(i == 1) { h = temp; temp->preview = NULL; } temp->preview = p; p = temp; } p->next = h; h->preview = p; free(q); p = h; int j = 1; int k = TOTAL; printf("/nout:"); while(k > 2) { if(j == LOOP) { if(h == p) { h = p->next; } printf("% d", p->num); p->preview->next = p->next; p->next->preview = p->preview; j = 0; k--; free(p); } j++; p = p->next; } p = h; printf("/nleft:"); int lefts = LOOP - 1; for(i = 0; i < lefts; i++) { printf(" %d", p->num); free(p); p = p->next; } printf("/n"); return 0; }

你可能感兴趣的:(面试,struct,null)