Biorhythms 深藏阁楼爱情的钟 2022-05-25 13:48 111阅读 0赞 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. 代码: 方法一: #include<stdio.h> int main() { int i,j,n,m,k,t,p,e,d; n=1; while(scanf("%d %d %d %d",&p,&e,&i,&d)&&(p!=-1||e!=-1||i!=-1&&d!=-1)) { p=p%23; e=e%28; i=i%33; m=0; for(j=0;;j++) { m=(j*33)+i; if(m>d&&m%23==p&&m%28==e) break; } m-=d; if(m>21252) m=21252; if(m==1) printf("Case %d: the next triple peak occurs in %d day.\n",n,m); else printf("Case %d: the next triple peak occurs in %d days.\n",n,m); n++; } return 0; } 方法二:(转自[点击打开链接][Link 1]) 大致题意: 这题在POJ上有译文(原文右上角) 解题思路: 中国剩余定理,本题难点不在编程,而是分析题目并转化为数学公式 要引入本题解法,先来看一个故事 “韩信点兵”: 传说西汉大将韩信,由于比较年轻,开始他的部下对他不很佩服。有一次阅兵时,韩信要求士兵分三路纵队,结果末尾多2人,改成五路纵队,结果末尾多3人,再改成七路纵队,结果又余下2人,后来下级军官向他报告共有士兵2395人,韩信立即笑笑说不对(因2395除以3余数是1,不是2),由于已经知道士兵总人数在2300~2400之间,所以韩信根据23,128,233,------,每相邻两数的间隔是105(3、5、7的最小公倍数),便立即说出实际人数应是2333人(因2333=128+20χ105+105,它除以3余2,除以5余3,除以7余2)。这样使下级军官十分敬佩,这就是韩信点兵的故事。 韩信点兵问题简化:已知 n%3=2, n%5=3, n%7=2, 求n。 再看我们这道题,读入p,e,i,d 4个整数 已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n 。 两道题是一样的。但是韩信当时计算出结果的? 韩信用的就是“中国剩余定理”,《孙子算经》中早有计算方法,大家可以查阅相关资料。 “韩信点兵”问题计算如下: 因为n%3=2, n%5=3, n%7=2 且 3,5,7互质 (互质可以直接得到这三个数的最小公倍数) 令x= n%3=2 , y= n%5=3 ,z= n%7=2 使5×7×a被3除余1,有35×2=70,即a=2; 使3×7×b被5除余1,用21×1=21,即b=1; 使3×5×c被7除余1,用15×1=15,即c=1。 那么n =(70×x+21×y+15×z)%lcm(3,5,7) = 23 这是n的最小解 而韩信已知士兵人数在2300~2400之间,所以只需要n+i×lcm(3,5,7)就得到了2333,此时i=22 同样,这道题的解法就是: 已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i 使33×28×a被23除余1,用33×28×8=5544; 使23×33×b被28除余1,用23×33×19=14421; 使23×28×c被33除余1,用23×28×2=1288。 因此有(5544×p+14421×e+1288×i)% lcm(23,28,33) =n+d 又23、28、33互质,即lcm(23,28,33)= 21252; 所以有n=(5544×p+14421×e+1288×i-d)%21252 本题所求的是最小整数解,避免n为负,因此最后结果为n= \[n+21252\]% 21252 那么最终求解n的表达式就是: n=(5544\*p+14421\*e+1288\*i-d+21252)%21252; 当问题被转化为一条数学式子时,你会发现它无比简单。。。。直接输出结果了。 #include<iostream> using namespace std; int main(void) { int p,e,i,d; int time=1; while(cin>>p>>e>>i>>d) { if(p==-1 && e==-1 && i==-1 && d==-1) break; int lcm=21252; // lcm(23,28,33) int n=(5544*p+14421*e+1288*i-d+21252)%21252; if(n==0) n=21252; cout<<"Case "<<time++<<": the next triple peak occurs in "<<n<<" days."<<endl; } return 0; } [Link 1]: https://user.qzone.qq.com/289065406/blog/1309411846
相关 poj 1006 Biorhythms(中国剩余定理水题) 题目衔接:[http://poj.org/problem?id=1006][http_poj.org_problem_id_1006] > <table> > <... た 入场券/ 2024年04月19日 15:26/ 0 赞/ 47 阅读
相关 【CRT】Biorhythms [E-Biorhythms\_牛客竞赛数学专题班同余与模(逆元、费马小定理、欧拉定理、孙子定理) (nowcoder.com)][E-Biorhythms_ _nowcoder 怼烎@/ 2024年03月22日 16:14/ 0 赞/ 43 阅读
相关 POJ 1006 Biorhythms Biorhythms <table> <tbody> <tr> <td><strong>Time Limit:</strong> 1000MS</ 拼搏现实的明天。/ 2022年09月30日 03:42/ 0 赞/ 131 阅读
相关 Poj1006 Biorhythms (中国剩余定理) 今年网络赛用到的知识点,补习中…… 题意:人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天。一个周期内有一天为峰值,在这一天,人在对应的方面(体力,情感或 我就是我/ 2022年09月17日 06:23/ 0 赞/ 143 阅读
相关 POJ 1006 Biorhythms 中国剩余定理 引子 韩信点兵 韩信命令士兵3人一排,结果多出2名; 紧接着命令士兵5人一排,结果多出3名; 他又命令士兵7人一排,结果又多出 悠悠/ 2022年09月05日 13:55/ 0 赞/ 114 阅读
相关 Biorhythms Description Some people believe that there are three cycles in a person's life that sta 深藏阁楼爱情的钟/ 2022年05月25日 13:48/ 0 赞/ 112 阅读
相关 POJ 1006 Biorhythms 模拟 Biorhythms <table> <tbody> <tr> <td><strong>Time Limit:</strong> 1000MS</ 川长思鸟来/ 2022年02月24日 10:58/ 0 赞/ 179 阅读
还没有评论,来说两句吧...