Biorhythms
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 86386 |
|
Accepted: 26207 |
Description
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.
Input
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.
Output
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.
Sample Input
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1
Sample Output
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.
Source
题意:p e i分别代表的是三个高峰出现的时间(该年的第几天)d是给出的该年的第几天,让你从这一天开始求到它下一次三个同时高峰所经历的天数
解题思路:先确定一天的高峰期,然后每次循环都加23(确保那天是p的高峰期),再去判断是否e,i的高峰期
易错:循环开始前的d++必须的,因为是算下一次三个同高峰 (0 0 0 0可知)
#include <stdio.h> int main(int argc, char *argv[])
{
int p,e,i,d;
int cas=0;
int date;
while(scanf("%d%d%d%d",&p,&e,&i,&d))
{
if (p+e+d+i==-4)
break;
date=d;
d++;
while((d-p)%23)
{
d++;
}
while((d-e)%28!=0 || (d-i)%33!=0)
{
d+=23;
}
printf("Case %d: the next triple peak occurs in %d days.\n",++cas,d-date);
}
return 0;
}
法二:中国剩余定理(孙子定理)
在中国古代著名数学著作《孙子算经》中,有一道题目叫做“物不知数”,原文如下:
有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?
即,一个整数除以三余二,除以五余三,除以七余二,求这个整数。
中国数学家秦九韶于1247年做出了完整的解答,口诀如下
三人同行七十希,五树梅花廿一支,七子团圆正半月,除百零五使得知
这个解法实际上是,首先利用秦九韶发明的大衍求一术求出5和7的最小公倍数35的倍数中除以3余数为1的最小一个70(这个称为35相对于3的数论倒数),3和7的最小公倍数21相对于5的数论倒数21,3和5的最小公倍数15相对于7的数论倒数15。然后
![70 \times 2 + 21 \times 3 + 15 \times 2 = 233](http://upload.wikimedia.org/wikipedia/zh/math/e/1/f/e1fa3cbd7841d9617a0942ef34df5d03.png)
233便是可能的解之一。它加减3、5、7的最小公倍数105的若干倍仍然是解,因此最小的解为233除以105的余数23。
附注:这个解法并非最简,因为实际上35就符合除3余2的特性,所以最小解是:
最小解加上105的正整数倍都是解。
例1:一个数被3除余1,被4除余2,被5除余4,这个数最小是几?题中3、4、5三个数两两互质。则〔4,5〕=20;〔3,5〕=15;〔3,4〕=12;〔3,4,5〕=60。为了使20被3除余1,用20×2=40;使15被4除余1,用15×3=45;使12被5除余1,用12×3=36。然后,40×1+45×2+36×4=274,因为,274>60,所以,274-60×4=34,就是所求的数。
例2:一个数被3除余2,被7除余4,被8除余5,这个数最小是几?题中3、7、8三个数两两互质。则〔7,8〕=56;〔3,8〕=24;〔3,7〕=21;〔3,7,8〕=168。为了使56被3除余1,用56×2=112;使24被7除余1,用24×5=120。使21被8除余1,用21×5=105;然后,112×2+120×4+105×5=1229因为,1229>168,所以,1229-168×7=53【1229%168】,,就是所求的数。
#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;
}