ITeye博客

//pku1006 源代码分析

fullfocus · 2007-11-01 10:51 · 算法和数据结构 · Markdown

java 代码

 

  1. //pku1006 源代码分析
  2. //注释掉的是第一次写的,用的是穷举的方法,输出结//果都正确,但是time limited,想想也是哦,这么
  3. //做,三次循环,不超时才怪啊,
  4. //现在的代码采用逆推方法,一个循环,快多了:)
  5. //总结:写代码,首要的是解题思路要对头,要高效,//然后有可能才考虑代码优化,性能提高的问题!!!//切记!!
  6. package com.pku;
  7. import java.io.*;
  8. import java.util.*;
  9. public class Pku1006 {
  10. /**
  11. * @param args
  12. */
  13. public static void main(String[] args) {
  14. // TODO Auto-generated method stub
  15. Scanner cin = new Scanner(new BufferedInputStream(System.in));
  16. int p = 0;
  17. int e = 0;
  18. int i = 0;
  19. int d = 0;
  20. int n = 0;
  21. int t = 1;
  22. while(cin.hasNext())
  23. {
  24. p = cin.nextInt();
  25. e = cin.nextInt();
  26. i = cin.nextInt();
  27. d = cin.nextInt();
  28. if(p == -1) break;
  29. else
  30. {
  31. System.out.print("Case"+ ' '+ t + ": the next triple peak occurs in ");
  32. for(n = d+1; n<=21253 + d;n++)
  33. {
  34. if(Math.abs(n-p)%23==0&&Math.abs(n-e)%28==0&&Math.abs(n-i)%33==0)
  35. {
  36. System.out.println((n-d)+ " days.");
  37. break;
  38. }
  39. }
  40. }
  41. t++;
  42. }
  43. }
  44. }
  45. //有的时候是思想方法不多,才使程序通不过,只有思想方法,解题方法正确以后,才考虑程序的优化问题,才有意义!
  46. // 自已编写的可以得出正确结果,但是TIME LIMITED~:(
  47. // int nextPeak(int p, int e, int i,int d)
  48. // {
  49. // int x = 0;
  50. // for(x = 1; x < (21252+365)/23; x++)
  51. // for(int y=1; y < (21252+365)/28; y++)
  52. // for(int z=1; z < ((21252+365)/33); z++)
  53. // {
  54. // if((p+23*x) == (e + 28*y )&& (e + 28*y) == (i + 33*z))
  55. // {
  56. // if( p + 23*x <= d)
  57. // return nextPeak(d + 23*x ,d + 23*x ,d + 23*x ,d);
  58. // else return p +23*x -d;
  59. // }
  60. // }
  61. // return p +23*x -d;
  62. // }
  63. // public static void main(String[] args) {
  64. // // TODO Auto-generated method stub
  65. // Scanner cin = new Scanner(new BufferedInputStream(System.in));
  66. // Pku1006 pku1006 = new Pku1006();
  67. // int p = 0;
  68. // int e = 0;
  69. // int i = 0;
  70. // int d = 0;
  71. // int days = 0;
  72. // int k = 1;
  73. // int first = 0;
  74. // while(cin.hasNext())
  75. // {
  76. // if( (first = cin.nextInt())== -1)
  77. // {
  78. // if(cin.nextInt()== -1&&cin.nextInt()== -1&&cin.nextInt()== -1)
  79. // {
  80. //
  81. // return;
  82. // }
  83. // }
  84. // else{
  85. // p = first;
  86. // e = cin.nextInt();
  87. // i = cin.nextInt();
  88. // d = cin.nextInt();
  89. // days = pku1006.nextPeak(p,e,i,d);
  90. // System.out.println("Case "+k+": the next triple peak occurs in "+days+" days.");
  91. // k++;
  92. // }
  93. // }
  94. //
  95. // }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。