Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
两段代码的输出方式略有不同,但都通过了,有一点诧异/*第一段,以-1 -1 -1 -1作为结束标志,一次输出所有结果*/ # include <stdio.h> # define PHYSICAL 23 # define EMOTIONAL 28 # define INTELLECTUAL 33 # define BIG_CYCLE 21252 typedef struct caseNode { int days; struct caseNode * next; }CASE_NODE; typedef struct { CASE_NODE *head; CASE_NODE *rear; }CASE; CASE * CreateCase() { CASE *c; c = malloc(sizeof(CASE)); c->head = malloc(sizeof(CASE_NODE)); c->rear = c->head; return c; } void AddCase(CASE * c, int days) { CASE_NODE *cn; cn = malloc(sizeof(CASE_NODE)); cn->days = days; cn->next = NULL; c->rear->next = cn; c->rear = cn; } void DeleteCase(CASE *c) { CASE_NODE *cn, *p; cn = c->head; while(cn != c->rear) { p = cn; cn = cn->next; free(p); } free(cn); free(c); } void PrintCase(CASE *c) { CASE_NODE *p; int i = 1; p = c->head->next; while(p != NULL) { printf("Case %d: the next triple peak occurs in %d days.\n", i, p->days); p = p->next; i++; } } int main() { int p, e, i, d, x; CASE *c; c = CreateCase(); for(;;) { scanf("%d%d%d%d", &p, &e, &i, &d); if(p < 0 || e < 0 || i < 0 || d < 0) break; p %= PHYSICAL; e %= EMOTIONAL; i %= INTELLECTUAL; x = i; while(!(((x-p)%PHYSICAL==0)&&((x-e)%EMOTIONAL==0))) x += INTELLECTUAL; x -= d; if(x <= 0) x += BIG_CYCLE; AddCase(c, x); } PrintCase(c); DeleteCase(c); } /*第二段,得到一组数据就计算并输出一次结果,以-1 -1 -1 -1为标志退出程序*/ # include <stdio.h> # define PHYSICAL 23 # define EMOTIONAL 28 # define INTELLECTUAL 33 # define BIG_CYCLE 21252 int main() { int p, e, i, d, x, index; index = 1; for(;;) { scanf("%d%d%d%d", &p, &e, &i, &d); if(p < 0 || e < 0 || i < 0 || d < 0) break; p %= PHYSICAL; e %= EMOTIONAL; i %= INTELLECTUAL; x = i; while(!(((x-p)%PHYSICAL==0)&&((x-e)%EMOTIONAL==0))) x += INTELLECTUAL; x -= d; if(x <= 0) x += BIG_CYCLE; printf("Case %d: the next triple peak occurs in %d days.\n", index, x); index++; } return 0; } 哪位能解释一下为什么? Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator