This problem has the solution of the direct set formula
An O (n) solution is provided here.
Package practice;import Java.io.bufferedinputstream;import java.util.Scanner;/** * * * * @author Caiyu * @date 2014-11-4*/ Public classPOJ1006 { Public Static voidMain (string[] args) {Scanner cin=NewScanner (NewBufferedinputstream (System.inch)); while(true) { intp =Cin.nextint (); if(P <0) Break; intE =Cin.nextint (); if(E <0) Break; inti =Cin.nextint (); if(I <0) Break; intD =Cin.nextint (); if(D <0) Break; intx =1, y =1, z =1; intTy =0, TX =0, TZ =0; Boolean FX=true, FY =true, FZ =true; inttemp =0; while(true) { if(FY) {if(Ty <21252) Ty= e + -* y++-D; Else Break; if((ty-p + D)% at==0) { if(Ty >temp) {Temp=Ty; FZ=true; FX=true; FY=false; } Else if(Ty = =temp) {FY=false; } } } if(FZ) {if(TZ <21252) TZ= i + -* z++-D; Else Break; if((tz-p + D)% at==0) { if(TZ >temp) {Temp=TZ; FX=true; FY=true; FZ=false; } Else if(TZ = =temp) {FZ=false; } } } if(FX) {if(TX <21252) TX= p + at* x + +-D; Else Break; if((tx-e + D)% -==0) { 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 ."); } }}
Algorithm: POJ1006 Triple peak problem