大家好,又见面了,我是全栈君。
Description
Input
当p = e = i = d = -1时,输入数据结束。
Output
采用以下格式:
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
Sample Output
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.
刚开始按照自己的思路,就是用了三个嵌套循环遍历所有可能出现的情况,但是三个周期总是没有相等的值,不知错在哪里,代码如下,希望大神研究研究:
import java.util.Scanner;
public class Biorhythms {
static int pp = 23;
static int ee = 28;
static int ii = 33;
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int all = in.nextInt();
int[] array = new int[all * 4];
for (int i = 0; i < array.length; i++) {
array[i] = in.nextInt();
}
int days = 0;
for (int j = 0; j < all; j++) {
int p = array[4 * j];
int e = array[4 * j + 1];
int i = array[4 * j + 2];
int d = array[4 * j + 3];
days = getDays(p, e, i, d);
System.out.println("Case " + (j + 1)
+ ": the next triple peak occurs in " + days + " days.");
}
}
public static int getDays(int ps, int es, int is, int ds) {
int y = 0;
int p = ps;
int e = es;
int i = is;
int d = ds;
System.out.println(" p-->" + p + " e-->" + e + " i-->" + i);
for (; p < 21252; p = p + pp) {
for (; e < p; e = e + ee) {
for (; i < e; i = i + ii) {
System.out.println(" p-->" + p + " e-->" + e + " i-->" + i);
if (p == e && p == i && e == i) {
y = p - d;
break;
}
}
}
}
return y;
}
}
自己没做出来,就百度了一下资料,结果如下了,比较正规的解法:
这是一道典型的中国剩余定理(孙子定理)题,百科地址http://baike.baidu.com/link?url=vupDWKyEG0vb-4vi7PtvRsXV-wrWd0O8qg1Dx8SzlaFC6bt0fRi_0X9kkiXNWrFPJ5ID3kSE5K7WBuPFMBSaeei2PIFb4qYWv7PWn98A9xdR3c1oZJuYev_MJuwMipJ0BXOAGRdJCp8X5NjkfTGxhzRLU6iPdxwbrToG-gKBaNS
根据题意其实就是一个数被23整除余数是Lp=p % 23(p被23整除的余数),被28整除余数是Le=e % 28((e被28整除的余数),被33整除余数是Li=i % 33(i被33整除的余数),求这个数的最小正整数。这里之说以要p % 23而不是直接p是因为题目中提示并不是第一次到达最佳状态。
import java.util.Scanner;
public class Main{
public static void main(String[] args)throws Exception{
Scanner cin=new Scanner(System.in);
int m=1;
while(cin.hasNext()){
int p=0,e=0,i=0,d=0,lp=1,le=1,li=1,finalNum=0;
p=cin.nextInt() % 23;
e=cin.nextInt() % 28;
i=cin.nextInt() % 33;
d=cin.nextInt();
if(p==-1||e==-1||i==-1||d==-1)break;
while((924*lp) % 23!=1){
lp++;
}
while((759*le) % 28!=1){
le++;
}
while((644*li) % 33!=1){
li++;
}
finalNum=(924*lp*p+759*le*e+644*li*i) % 21252;
System.out.println(finalNum);
if(finalNum==d){
finalNum=21252;
}else if(finalNum<d){
finalNum=21252-(d-finalNum);
}else if(finalNum>d){
finalNum=finalNum-d;
}
System.out.println("Case "+m+": the next triple peak occurs in "+finalNum+" days.");
m++;
}
}
}
作者:jason0539
微博:http://weibo.com/2553717707
博客:http://blog.csdn.net/jason0539(转载请说明出处)
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/121383.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...