java 代码
- //pku1006 源代码分析
- //注释掉的是第一次写的,用的是穷举的方法,输出结//果都正确,但是time limited,想想也是哦,这么
- //做,三次循环,不超时才怪啊,
- //现在的代码采用逆推方法,一个循环,快多了:)
- //总结:写代码,首要的是解题思路要对头,要高效,//然后有可能才考虑代码优化,性能提高的问题!!!//切记!!
- 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++;
- }
- }
- }
- //有的时候是思想方法不多,才使程序通不过,只有思想方法,解题方法正确以后,才考虑程序的优化问题,才有意义!
- // 自已编写的可以得出正确结果,但是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++;
- // }
- // }
- //
- // }