//pku1006 源代碼分析
//注釋掉的是第一次寫的,用的是窮舉的方法,輸出結//果都正確,但是time limited,想想也是哦,這么
//做,三次循環(huán),不超時才怪啊,
//現(xiàn)在的代碼采用逆推方法,一個循環(huán),快多了:)
//總結:寫代碼,首要的是解題思路要對頭,要高效,//然后有可能才考慮代碼優(yōu)化,性能提高的問題!!!//切記!!
package com.pku;
import java.io.*;
import java.util.*;
public class Pku1006 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(new BufferedInputStream(System.in));
int p = 0;
int e = 0;
int i = 0;
int d = 0;
int n = 0;
int t = 1;
while(cin.hasNext())
{
p = cin.nextInt();
e = cin.nextInt();
i = cin.nextInt();
d = cin.nextInt();
if(p == -1) break;
else
{
System.out.print("Case"+ ' '+ t + ": the next triple peak occurs in ");
for(n = d+1; n<=21253 + d;n++)
{
if(Math.abs(n-p)%23==0&&Math.abs(n-e)%28==0&&Math.abs(n-i)%33==0)
{
System.out.println((n-d)+ " days.");
break;
}
}
}
t++;
}
}
}
//有的時候是思想方法不多,才使程序通不過,只有思想方法,解題方法正確以后,才考慮程序的優(yōu)化問題,才有意義!
// 自已編寫的可以得出正確結果,但是TIME LIMITED~:(
// int nextPeak(int p, int e, int i,int d)
// {
// int x = 0;
// for(x = 1; x < (21252+365)/23; x++)
// for(int y=1; y < (21252+365)/28; y++)
// for(int z=1; z < ((21252+365)/33); z++)
// {
// if((p+23*x) == (e + 28*y )&& (e + 28*y) == (i + 33*z))
// {
// if( p + 23*x <= d)
// return nextPeak(d + 23*x ,d + 23*x ,d + 23*x ,d);
// else return p +23*x -d;
// }
// }
// return p +23*x -d;
// }
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// Scanner cin = new Scanner(new BufferedInputStream(System.in));
// Pku1006 pku1006 = new Pku1006();
// int p = 0;
// int e = 0;
// int i = 0;
// int d = 0;
// int days = 0;
// int k = 1;
// int first = 0;
// while(cin.hasNext())
// {
// if( (first = cin.nextInt())== -1)
// {
// if(cin.nextInt()== -1&&cin.nextInt()== -1&&cin.nextInt()== -1)
// {
//
// return;
// }
// }
// else{
// p = first;
// e = cin.nextInt();
// i = cin.nextInt();
// d = cin.nextInt();
// days = pku1006.nextPeak(p,e,i,d);
// System.out.println("Case "+k+": the next triple peak occurs in "+days+" days.");
// k++;
// }
// }
//
// }
Pku1006
posted on 2007-11-01 10:41
fullfocus 閱讀(777)
評論(0) 編輯 收藏