POJ 1006 Biorhythms
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.
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.
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
题目大致意思:人的一生有三个周期,分别是物理、情绪、智力,周期分别是23, 28, 33天,达到这个时期的时候人的某一项会变得特别敏感,现在给出三个周期第一次发生的日期,求下一次三个周期同时发生的日期。
方法一:我们可以采取逐个击破的策略,就是我们先求其中两个周期必定相遇的时间,然后这个两个周期化作一个新的周期,它的顶峰时间是LCK(x,y)//两个数的最小公倍数,然后再求这个新的周期和另外一个周期的相遇时间就OK了。
代码如下:
#include<iostream> using namespace std; void Meet(int p,int e,int i,int day) { p %= 23, e %= 28, i %= 33; while ((i - e) % 28) i+=33; //求出i e相遇的日期 while ((i - p) % 23) i += 924; //求出ie p 相遇的日期 924是i e的最小公倍数 加924保证i e一定相遇 if (i <= day) cout << 21252 - (day - i) << endl; //避免出现负数 else cout << i - day << endl; } int main() { int p, e, i, day; while (cin >> p >> e >> i >> day) { if (p == e == i == day == -1) break; Meet(p, e, i, day); } }
方法二:我们也可以把这个题目看作是三个运动员的跑步相遇问题。那么我们求的就是他们相遇的时候一共经过的总路程。我们当然让落在最后面的先跑,每跑一次就判断一下是否相遇,相遇了就输出结果,否则找到落在后面的,继续跑。
代码如下:
#include<iostream> using namespace std; int min(int a, int b) { return a > b ? b : a; } void Meet(int p,int e,int i,int day) { 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 <= day) cout << 21252 - (day - p) << endl; //避免出现负数 不能用p直接减day else cout << p - day << endl; } int main() { int p, e, i, day; while (cin >> p >> e >> i >> day) { if (p == e == i == day == -1) break; Meet(p, e, i, day); } }