#include #include #include typedef struct node { struct node *next; int i; } node; static node *head = NULL; static node *tail = NULL; static char *ReadLine(char *p, size_t size, FILE *fp) { if ((p=fgets(p, size, fp))) { size_t l = strlen(p); while(l && p[l-1] == '\n') { p[--l] = 0; } } return p; } static void Add(int i) { node *new; new = malloc(sizeof *new); new->next = NULL; if (tail) { tail->next = new; } tail = new; if (!head) { head = new; } new->i = i; } int main(void) { char buff[1024]; unsigned long long num_count = 0; char *p = NULL; unsigned long long f = 0; int cycle = 0; ReadLine(buff, sizeof buff, stdin); p = strtok(buff, ","); while(p) { Add(atoi(p)); p = strtok(NULL, ","); } for(cycle = 0; cycle < 256; cycle++) { int to_add = 0; node *list; list = head; while(list) { if (list->i == 0) { to_add++; list->i = 6; } else { list->i--; } list = list->next; } if (to_add) { num_count += to_add; for(f = 0; f < to_add; f++) { Add(8); } } printf("cycle %d/%llu\n", cycle, num_count); } printf("num_count=%llu\n", num_count); return 0; }