码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1006

时间:2015-11-27 21:34:54      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

POJ 1006 生物周期
限制:1s 10M
人有三种周期,分别是身体,情绪和智力,周期分别是23,28,33天。

每个周期中有一个峰值,在峰值时,在对应领域表现最佳(身体,情绪或智力)。
因为三个周期不同,所以峰值发生在不同时间,需要知道对于一个人三种峰值出现在同一天的时间。

对于某一个周期,给定从当前年初到该周期峰值需经历的天数(不必是第一个峰值);同时给定日期(表示从当前年初到现在经历的天数)。

任务是:找出给定日期到下个三重峰值经历的天数。如果一个三峰值发生在给定的日期,需要给出距离下次三重峰值发生的天数。

输入:
每个测试样例为一行,包含四个数字。整数p,e,i,d
p,e,i分别为从当前年初到身体,情绪,智力峰值的天数,d是给定的日期,给定的日期可能小于p,e,i的周期;所有的输入为非负数,最大为365,可假设一个三峰值会在23*28*33 = 21252 天内发生。
输入的结束将会给出一行:p=e=i=d=-1

输出:
对每个测试样例,打印样例数字,和距离下次三峰值的天数。
例如:
Case 1: the next triple peak occurs in 1234 days.

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.

核心思想:

中国剩余定理
m1 m2 .. mr 为两两互素正整数
x = a1(mod m1)
x = a2(mod m2)
x = ar(mod mr)
则该方程组在模 M = m1 * m2 * ...* mr下有唯一解
唯一解的形式 x = a1*M1*y1 + a2*M2*y2 + .. + ar*Mr*yr
其中:
Mk*yk=1(mod mk)
Mk = M/mk

解出yk后,带入唯一解形式求出x

本题中,
若x等于0,需要返回23*28*33=21252
若x小于0,则需要加上21252

C代码:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 int main()
 5 {
 6     int p,e,i,d,x,casenum = 0;
 7     while(scanf("%d%d%d%d", &p,&e,&i,&d) != EOF)
 8     {
 9         if (p == -1)
10             break;
11 
12         x = (5544*p + 14421*e + 1288*i - d + 21252) % 21252;
13         if (x==0)
14             x = 21252;
15 
16         printf("Case %d: the next triple peak occurs in %d days.\n", ++casenum, x);
17 
18     }
19     return 0;
20 }

 

POJ 1006

标签:

原文地址:http://www.cnblogs.com/tobebrave/p/5001574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!