C语言起步之poj.org竞赛试题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. 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 |
自我练习:
根据题目要求,他需要我们给定一个天数,求他们下一天同样是在一起的下一天,设置一个m则m最大为21252=23*28*33,
由于p+n1*23=m
e+n2*28=m
i+n3*33=m
有n1,n2,n3,m,四个变量三个方程,显然不能解,故让m从1到21252变化。
利用dev c++编译器来编写源程序:
但这样编写的源代码
做出来输入几组数据发现问题所在:
input: 0 0 0 0
input: 30 30 30 30
有三个相同时,程序运行到m=p=i=e时就会等于m-d。
经过改正,将p=e=i=-1结束这一条件加入进去,利用中国剩余定理:(取自百度百科)
用现代数学的语言来说明的话,中国剩余定理给出了以下的一元线性同余方程组:
有解的判定条件,并用构造法给出了在有解情况下解的具体形式。
设
是整数m1,m2, ... ,mn的乘积,并设
是除了mi以外的n- 1个整数的乘积。
设
为
模
的数论倒数(
为
模
意义下的逆元)
方程组
的通解形式为
在模
的意义下,方程组
只有一个解:
从假设可知,对任何
,由于
,所以
这说明存在整数
使得
这样的
叫做
模
的数论倒数。考察乘积
可知:
所以
满足:
这说明
就是方程组
的一个解。
另外,假设
和
都是方程组
的解,那么:
而
两两互质,这说明
整除
. 所以方程组
的任何两个解之间必然相差
的整数倍。而另一方面,
是一个解,同时所有形式为:
的整数也是方程组
的解。所以方程组所有的解的集合就是:
方便理解:例
除以7余2的数可以写成7n+2。
7n+2这样的数除以8余4,由于2除以8余2,所以要求7n除以8余2。
7n除以8余2,7除以8余7,要求n除以8余6(乘数之余等于余数之乘),则n最小取6。
所以满足“除以7余2,除以8余4”的最小的数是7×6+2=44,
所有满足“除以7余2,除以8余4”的数都可以写成44+56×m。
要求44+56×m除以9余3,由于44除以9余8,所以要求56×m除以9余4。(加数之余等于余数之加)
56×m除以9余4,由于56除以9余2,所以要求m除以9余2(乘数之余等于余数之乘),则m最小取2。
所以满足“除以7余2,除以8余4,除以9余3”的最小的数是44+56×2=156。
程序如下:
输入各种数据结果如下:
成功找出下一个高峰,初次学习,继续加油。