fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define N 9 /* No. de nohs */
  5. #define M 5 /* No. de saltos */
  6.  
  7. typedef struct node* link;
  8.  
  9. struct node {
  10. int item;
  11. link next;
  12. };
  13.  
  14. /* Funcao principal */
  15. int main() {
  16. printf("==============================================\n");
  17. printf(" PROBLEMA DE FLAVIO JOSEFO (lista circular) \n");
  18. printf("==============================================\n");
  19. printf("\n");
  20.  
  21. int i; /* Contador */
  22. int n = N; /* No. de nohs */
  23.  
  24. // link t = malloc(sizeof *t);
  25. link t = malloc(sizeof(link));
  26. link x = t;
  27.  
  28. /* Criacao do noh cabeca da lista circular:
  29.   ele aponta para si mesmo */
  30. t->item = 1;
  31. t->next = t;
  32.  
  33. printf("size of x: %d, size of link: %d\n", (sizeof *t), (sizeof(link)));
  34.  
  35. /* Preenchimento do restante da lista circular */
  36. for (i = 2; i <= N; i++) {
  37. x = (x->next = malloc(sizeof *x));
  38. printf("x: %p, x->next: %p, t: %p\n", x, x->next, t);
  39. x->item = i;
  40. x->next = t;
  41. }
  42.  
  43. /* Eliminacao dos nohs */
  44. while (x != x->next) {
  45. for (i = 1; i < M; i++)
  46. x = x->next;
  47. x->next = x->next->next;
  48. n--;
  49. }
  50.  
  51. printf("Noh restante: %d\n", x->item);
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0s 5548KB
stdin
Standard input is empty
stdout
==============================================
 PROBLEMA DE FLAVIO JOSEFO (lista circular) 
==============================================

size of x: 16, size of link: 8
x: 0x5645b3f70290, x->next: (nil), t: 0x5645b3f70270
x: 0x5645b3f702b0, x->next: (nil), t: 0x5645b3f70270
x: 0x5645b3f702d0, x->next: (nil), t: 0x5645b3f70270
x: 0x5645b3f702f0, x->next: (nil), t: 0x5645b3f70270
x: 0x5645b3f70310, x->next: (nil), t: 0x5645b3f70270
x: 0x5645b3f70330, x->next: (nil), t: 0x5645b3f70270
x: 0x5645b3f70350, x->next: (nil), t: 0x5645b3f70270
x: 0x5645b3f70370, x->next: (nil), t: 0x5645b3f70270
Noh restante: 8