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.
问题的实际和我前面一篇博客的时针,分针和秒针相遇的问题一样的。
有两个解题思路:
1 先计算两个周期相遇的日子,然后验证这个日子是否和第三个周期相遇
2 把三个周期看成三个跑步者,跑到一个地点(日期)停下来,让最慢的先跑,看三者是否相遇,然后再让最慢的先跑,停下来,验证……如此循环
思路1的程序:
int runnersMeet(int p, int e, int i, int d) { p %= 23, e %= 28, i %= 33; while ((i - e) % 28) i += 33; while ((i - p) % 23) i += 924;//28*33 == 924 if (i <= d) return 21252 - (d - i); return i - d; }
第一个while循环是为了计算出i 和e相遇的日子的
更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
第二个whlle循环就是确定了i和e必定相遇的日子是i, i+924, i+924*2……然后验证这些日子是否也和p相遇
思路2程序:
int runnersMeet(int p, int e, int i, int d) { p %= 23, e %= 28, i %= 33; while (!(p == e && e == i)) { int m = min(p, min(e, i)); if (m == p) p += 23; else if (m == e) e += 28; else i += 33; } if (p <= d) return 21252 - (d - p);//注意公式计算别搞错了 return p - d; }
思路2好像更简单点,更好理解点。
最后主程序,就AC啦。
void Biorhythms() { int p,e,i,d, c = 0; while (cin>>p>>e>>i>>d && -1 != d) { c++; printf("Case %d: the next triple peak occurs in %d days.\n", c, runnersMeet(p, e, i, d)); } } int main() { Biorhythms(); return 0; }
感觉是第一个思路的程序走的快点,但是实际运行反而是第一个程序慢,呵呵,计算的时间复杂度都是一样的,oj系统计算的运行时间估计也不完全可靠吧。
两个思路的应用情况:
1 如果一个的速度和其他两个的速度相差很大的时候,比如时针,分钟和秒针的情况,秒针走快很多,那么就肯定是第一个思路快,
2 但是如果如本题的话,三者速度相差无几,那么使用加法,不用模操作,也许就是第二个思路快的原因吧。
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索next
, number
, 思路
, cycle
, of
, given)''
, The
, occurred
given
追及相遇问题解题思路、biorhythms、相遇周期、卫星相遇周期、程序设计思路,以便于您获取更多的相关知识。