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相遇的日子的

第二个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 可是假设如本题的话,三者速度相差无几。那么使用加法,不用模操作,或许就是第二个思路快的原因吧。

POJ - 1006 Biorhythms 周期相遇 两个思路程序的更多相关文章

  1. POJ 1006 生理周期(中国剩余定理)

    POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...

  2. POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

    POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...

  3. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  4. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  5. 中国剩余定理算法详解 + POJ 1006 Biorhythms 生理周期

    转载请注明出处:http://exp-blog.com/2018/06/24/pid-1054/ #include <iostream> #include <cstdio> u ...

  6. [POJ 1006] Biorhythms C++解题

        Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 107569   Accepted: 33365 ...

  7. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

  8. poj 1006 Biorhythms (中国剩余定理模板)

    http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...

  9. POJ 1006 Biorhythms (数论-中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 111285   Accepted: 34638 Des ...

随机推荐

  1. GDB调试精粹及使用实例

    一:列文件清单 1. List (gdb) list line1,line2 二:执行程序 要想运行准备调试的程序,可使用run命令,在它后面可以跟随发给该程序的任何参数,包括标准输入和标准输出说明符 ...

  2. Android 进程和线程

    进程和线程 如果某个应用程序组件是第一次被启动,且这时应用程序也没有其他组件在运行,则Android系统会为应用程序创建一个包含单个线程的linux进程.默认情况下,同一个应用程序的所有组件都运行在同 ...

  3. Swift--基本数据类型(一)

    不像更多语言中,X不要求你写一个分号(;)在你的代码中的每一个语句后,尽管能够这样做.然而,假设你想在一行中写入多个单独的语句分号是必需的: .    1  let cat = "" ...

  4. dhtmlx之dhtmlXGrid显示数据

    引用 <link href="../../dhtmlXGridScripts/dhtmlxgrid.css" rel="stylesheet" type= ...

  5. MVC 扩展 Html.ImageFor

    Asp.Net MVC 扩展 Html.ImageFor 方法详解 背景: 在Asp.net MVC中定义模型的时候,DataType有DataType.ImageUrl这个类型,但htmlhelpe ...

  6. QTableView 添加进度条 添加按钮 TreeWidget 增删改

    http://www.cnblogs.com/li-peng/p/3961386.html http://www.cnblogs.com/li-peng/p/3961843.html http://w ...

  7. 设计模式 ( 十三 ) 命令模式Command(对象行为型)

    设计模式 ( 十三 ) 命令模式Command(对象行为型) 1.概述         在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需 ...

  8. cocos2d-x游戏开发系列教程-超级玛丽02-代码结构

    代码下载链接 http://download.csdn.net/detail/yincheng01/6864893 解压密码:c.itcast.cn 前景回顾 上一篇博文提到超级马里奥的游戏效果,大家 ...

  9. WM_PAINT消息小结

    WM_PAINT是Windows窗口系统中一条重要的消息,应用程序通过处理该消息实现在窗口上的绘制工作. 1. 系统何时发送WM_PAINT消息? 系统会在多个不同的时机发送WM_PAINT消息:当第 ...

  10. 【Linux驱动器】Linux-2.6.20.4内核移植

    最近一段时间以来一直学习TQ2440内核开发板移植.嫁接驱动器. 真诚地相信这方面的知识有很大的困难,.但有一种观点认为,从看,难度越大,的提升空间的能力更大! ! 1.解压源代码 从Internet ...