1635 - Biorhythms
Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier.
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
题目输入
You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.
题目输出
For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form:
Case 1: the next triple peak occurs in 1234 days.
Use the plural form "days'' even if the answer is 1.
输入/输出样例
输入格式
8 6 34 24 42 45 65 2 3 0 0 8 -1 -1 -1 -1
输出格式
Case 1: the next triple peak occurs in 4630 days. Case 2: the next triple peak occurs in 9171 days. Case 3: the next triple peak occurs in 16624 days.
C语言解答
#include "stdio.h" int main() { int p,e,i,d,j,k=1; while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF) { if(p==-1&&e==-1&&i==-1&&d==-1) break; for(j=d+1;j<=21252+d;j++) { if((j-p)%23==0&&(j-e)%28==0&&(j-i)%33==0) { printf("Case %d: the next triple peak occurs in %d days.\n",k,j-d); k++; } } } return 0; }
C++解答
/*中国剩余定理(孙子定理) 在中国古代著名数学著作《孙子算经》中,有一道题目叫做“物不知数”,原文如下: 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何? 即,一个整数除以三余二,除以五余三,除以七余二,求这个整数。 中国数学家秦九韶于1247年做出了完整的解答,口诀如下 三人同行七十希,五树梅花廿一支,七子团圆正半月,除百零五使得知 这个解法实际上是,首先利用秦九韶发明的大衍求一术求出5和7的最小公倍数35的倍数中除以3余数为1的最小一个70(这个称为35相对于3的数论倒数),3和7的最小公倍数21相对于5的数论倒数21,3和5的最小公倍数15相对于7的数论倒数15。然后 233便是可能的解之一。它加减3、5、7的最小公倍数105的若干倍仍然是解,因此最小的解为233除以105的余数23。 附注:这个解法并非最简,因为实际上35就符合除3余2的特性,所以最小解是:最小解加上105的正整数倍都是解。*/ #include <stdio.h> int main() { int p,e,i,d,n; int num=1; while(1) { scanf("%d%d%d%d",&p,&e,&i,&d); if(p!=-1) { n=(5544*p+14421*e+1288*i-d)%21252; if(n<=0) n+=21252; printf("Case %d: the next triple peak occurs in %d days.\n",num++,n); } else break; } return 0; }