这题有直接套公式的解法

这里提供一个O(n)的解法。

package practice;

import java.io.BufferedInputStream;
import java.util.Scanner; /**
*
*
* @author caiyu
* @date 2014-11-4
*/
public class POJ1006 {
public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
while (true) {
int p = cin.nextInt();
if (p < )
break;
int e = cin.nextInt();
if (e < )
break;
int i = cin.nextInt();
if (i < )
break;
int d = cin.nextInt();
if (d < )
break;
int x = , y = , z = ;
int ty = , tx = , tz = ;
boolean fx = true, fy = true, fz = true;
int temp = ;
while (true) { if (fy) {
if (ty < )
ty = e + * y++ - d;
else
break;
if ((ty - p + d) % == ) {
if (ty > temp) {
temp = ty;
fz = true;
fx = true;
fy = false;
} else if (ty == temp) {
fy = false;
}
}
} if (fz) {
if (tz < )
tz = i + * z++ - d;
else
break;
if ((tz - p + d) % == ) {
if (tz > temp) {
temp = tz;
fx = true;
fy = true;
fz = false;
} else if (tz == temp) {
fz = false;
}
}
} if (fx) {
if (tx < )
tx = p + * x++ - d;
else
break;
if ((tx - e + d) % == ) {
if (tx > temp) {
temp = tx;
fy = true;
fz = true;
fx = false;
} else if (tx == temp) {
fx = false;
}
}
}
if (!fx && !fy && !fz)
break; } System.out.println("Case 1: the next triple peak occurs in " + temp
+ " days.");
} }
}
05-11 11:30