我使用的算法比较简单,就是循环,当出现(day-p) % 23 == 0 && (day-e) % 28 == 0 && (day-i) % 33 ==0表示当天是三个周期的高峰。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
int idx = 0;
while(true){
int p = cin.nextInt();
int e = cin.nextInt();
int i = cin.nextInt();
int d = cin.nextInt();
if(p == -1 && e == -1 && i==-1 && d==-1){
break;
}
idx++;
System.out.println("Case "+idx+": the next triple peak occurs in "+ getNextPeakDay(p, e, i, d)+" days.");
}
}
private static int getNextPeakDay(int p, int e, int i, int d){
int day = d;
while(true){
day++;
if((day-p) % 23 == 0 && (day-e) % 28 == 0 && (day-i) % 33 ==0){
break;
}
}
return day-d;
}
}
网上有另外一种解法,效率比较高,暂时没想出是怎么推导出的。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int k = 0;
while (scan.hasNext()) {
int p = scan.nextInt();
int e = scan.nextInt();
int i = scan.nextInt();
int d = scan.nextInt();
if (p == -1 && e == -1 && i == -1 && d == -1) {
break;
}
k++;
int days = (5544 * p + 14421 * e + 1288 * i - d) % (21252);
if (days <= 0) {
days = 23 * 28 * 33 + days;
}
System.out.println("Case " + k
+ ": the next triple peak occurs in " + days
+ " days.");
}
}
}
分享到:
相关推荐
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代码
POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析POJ 1006 源代码——中国剩余定理分析
解决poj1006问题
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
北大POJ1159-Palindrome 解题报告+AC代码
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
poj分类poj分类poj分类poj分类
北大POJ2002-Squares 解题报告+AC代码
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
POJ1503解答 POJ1503解答,正确答案(已通过POJ)
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
POJ1048,加强版的约瑟夫问题 难度中等
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
POJ1083的代码,POJ1083的代码,POJ1083的代码
poj 百练 题目分类 poj 百练 题目分类
POJ上的一道题目,自己写的代码,因为想下载别人的, 所以就放上了。