描述:
限制: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.
輸入樣例
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
輸出樣例
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.
思路
中國剩餘定理
則該方程組在模
唯一解的形式
其中:
解出
本題中,
若
若
C代碼:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int p,e,i,d,x,casenum = 0;
while(scanf("%d%d%d%d", &p,&e,&i,&d) != EOF)
{
if (p == -1)
break;
x = (5544*p + 14421*e + 1288*i - d + 21252) % 21252;
if (x==0)
x = 21252;
printf("Case %d: the next triple peak occurs in %d days.\n", ++casenum, x);
}
return 0;
}
參考:
http://blog.csdn.net/synapse7/article/details/9946013
http://blog.csdn.net/shanshanpt/article/details/8724769