fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. #define LEN 128
  6.  
  7. struct marble {
  8. long count;
  9. struct marble *next;
  10. struct marble *prev;
  11. } *mainroot, *root, *new, *old;
  12.  
  13. int main(void) {
  14. root=malloc(sizeof(struct marble));
  15. root->next = root->prev = root;
  16. root->count=0;
  17. mainroot = root;
  18. char line[LEN];
  19. long maxplayers;
  20. long maxpoints;
  21. fgets(line,LEN,stdin);
  22. sscanf(line,"%ld players; last marble is worth %ld points", &maxplayers, &maxpoints);
  23. printf("%ld %ld\n", maxplayers, maxpoints);
  24. exit(0);
  25. long player[maxplayers];
  26. memset(player,0,sizeof(player));
  27.  
  28. long currentmarble=1;
  29. long toppoint=0;
  30. long currentplayer=0;
  31. while(toppoint<maxpoints)
  32. {
  33. old=mainroot;
  34. while(old->next != mainroot)
  35. {
  36. printf("%ld ", old->count);
  37. old = old->next;
  38. }
  39. printf("\n");
  40.  
  41. if(currentmarble%23==0)
  42. {
  43. player[currentplayer] += currentmarble;
  44. for(int i=0;i<7;i++)
  45. root = root->prev;
  46. player[currentplayer] += root->count;
  47. (root->prev)->next = root->next;
  48. (root->next)->prev = root->prev;
  49. old = root;
  50. root = root->next;
  51. free(old);
  52. } else {
  53. new = malloc(sizeof(struct marble));
  54. new->next = (root->next)->next;
  55. root->next->next->prev = new;
  56. new->prev = root->next;
  57. (root->next)->next = new;
  58. new->count = currentmarble++;
  59. root = new;
  60. }
  61.  
  62. if(toppoint<player[currentplayer])
  63. toppoint=player[currentplayer];
  64.  
  65. currentplayer++;
  66. currentplayer%=maxplayers;
  67.  
  68. currentmarble++;
  69. }
  70.  
  71. printf("%ld\n",toppoint);
  72.  
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0s 5292KB
stdin
9 players; last marble is worth 30 points
stdout
9 30