POJ 1006 Biorhythms
这道题是一道数论题,运用到了所谓的“大衍求一术”。这个题目在 POJ 上是有中文版的,所以题目不是我翻译的。
原题地址:http://poj.org/problem?id=1006
POJ 1006 生理周期
问题描述
人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为 23 天、28 天和 33 天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为 10,下次出现三个高峰同天的时间是 12,则输出 2 (注意这里不是 3)。
输入数据
输入四个整数:p, e, i 和 d. p, e, i 分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于 p, e, 或 i. 所有给定时间是非负的并且小于 365, 所求的时间小于 21252.
当 p = e = i = d = -1 时,输入数据结束。
输出数据
从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。
采用以下格式:
Case 1: the next triple peak occurs in 1234 days.
注意:即使结果是1天,也使用复数形式“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.
解题思路
这道题懂了“大衍求一术”就可以做了。
源代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import java.util.*; public class Main { static final int a = 23; static final int b = 28; static final int c = 33; static final int a1 = 5544; // a1 MOD a == 1 static final int b1 = 14421; // b1 MOD b == 1 static final int c1 = 1288; // c1 MOD c == 1 static final int abc = 21252; public static void main(String[] args) { Scanner cin = new Scanner(System.in); int x = 0, y = 0, z = 0, w = 0; for (int i = 1 ; ; ++i) { x = cin.nextInt() % a; y = cin.nextInt() % b; z = cin.nextInt() % c; w = cin.nextInt(); if (w == -1) break; System.out.print("Case " + i + ": the next triple peak occurs in "); int days = (x * a1 + y * b1 + z * c1 - w) % abc; if (days == 0) System.out.print(abc); else if (days < 0) System.out.print(days + abc); else System.out.print(days); System.out.println(" days."); } } } |
原创文章,转载请注明来源:http://euyuil.com/2331/poj-1006-biorhythms/
