//pku1006 源代码分析

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值