我使用的算法比较简单,就是循环,当出现(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:深入解析与应用中国剩余定理》 在计算机编程领域,解决算法问题是我们提升技术能力的重要途径。POJ1006-Biorhythms是一个典型的算法挑战,它引入了数学中的一个重要概念——中国剩余定理。...
在压缩包文件的名称"BiorhythmsPoj1006"中,"Biorhythms"可能是指生物节律,这可能意味着问题与人的身体、情绪或智力状态的周期性变化有关。在编程竞赛中,这样的问题可能要求参赛者模拟或计算某种基于时间周期的...
* 1025 Biorhythms:这是一个动态规划题目,要求学习者编写一个程序来计算生物节律。 计算几何 计算几何是POJ题目中的一种重要题型,通常涉及计算几何算法的应用,如计算点的位置、计算多边形的面积等。这些题目...
7. 1005Biorhythms:涉及生物节律的计算,可能是周期性问题。 8. 1007DNASorting:该问题可能需要使用算法对DNA序列进行排序。 9. 1032Parliament:该题目可能是模拟议会投票或分配问题。 10. 1045BodePlot:这...
POJ(Problemset Online Judge)是一个在线编程竞赛平台,提供了大量的编程题目供参赛者练习和比赛。这个平台上的题目按照不同的主题和难度进行了分类,帮助参赛者有针对性地提高编程技能和算法理解。以下是一些主要...