对于这种循环的问题,使用模运算很容易解决。因此,我们可以将题目抽象成以下描述:对于每组p,e,i,d,设x是所求的天数,令y=x-d,则
y % 23 = p;
y % 28 = e;
y % 33 = i;
根据以上3个模方程,解出y,进而即可求出x。
对于方程组,我们可以使用枚举法来求出y。但是这里有更好的方法,应用中国剩余定理可以直接求解y。
①.找出在28和33的最小公倍数28*33的倍数中对23取模结果为1的数:最小的是5544
②.找出在23和33的最小公倍数23*33的倍数中对28取模结果为1的数:最小的是14421
③.找出在23和28的最小公倍数23*28的倍数中对33取模结果为1的数:最小的是1288
则 y = (5544*p + 14421*e + 1288*i)%21252(21252是23,28,33的最小公倍数)
在题目中使用的时候还要注意,这样的解有很多,但题目中限定了x的范围,x要大于0,且小于等于21252,因此这样的解只用1个。
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(new BufferedInputStream(System.in));
int p, e, i, d, x;
for(int j = 1; ; j++) {
p = in.nextInt();
if(p == -1)
break;
e = in.nextInt();
i = in.nextInt();
d = in.nextInt();
x = (5544*p + 14421*e + 1288*i)%21252;//中国剩余定理
if(x <= d)
x += 21252;
System.out.printf("Case %d: the next triple peak occurs in %d days.\n", j, x-d);
}
}
}
分享到:
相关推荐
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53539 Accepted: 15282 Description Some people believe that there are three cycles in a person's life that start the day he or ...
北大POJ1006-Biorhythms【中国剩余定理】 解题报告+AC代码
行业教育软件-学习软件-软件下载_学习软件_电脑学习_Natural Biorhythms 3.031免费下载.zip
小的 在这发个代码呵呵 北大ACM的1006题答案呵呵 帮助菜鸟啊呵呵
显示您与出生日期的照片墙(BioRhythms和所有那个...)的兼容性。百分;) 是的,不要忘记在设置中指定出生日期。 阅读更多关于Wiki或http://cmpati.me 愉快的使用! :) 用于搜索的标签:Fotostrana,BioRhythms,...
生物节律将在Cleve's Corner博客版中进行讨论。 请参阅http://blogs.mathworks.com/cleve/biorhythms 。
1160 Biorhythms 简单题 1163 The Staircases 简单题 1177 K-Magic Number 简单题 1184 Counterfeit Dollar 简单题 1182 Keeps Going and Going and ... 简单题,我喜欢:) 1284 Perfection 简单题 1272 ...
1160 Biorhythms 简单题 1163 The Staircases 简单题 1177 K-Magic Number 简单题 1184 Counterfeit Dollar 简单题 1182 Keeps Going and Going and ... 简单题,我喜欢:) 1284 Perfection 简单题 1272 ...