POJ1006 Java实现 By VinsmokeFy

Biorhythms

Time Limit: 1000MSMemory Limit: 10000K

Description

Some people believe that there are three cycles in a person’s life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier.
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.

Input

You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.

Output

For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form:
Case 1: the next triple peak occurs in 1234 days.
Use the plural form ``days’’ even if the answer is 1.

Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

Sample Output

Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

思路

一开始也没想那么多,就把p,e,i,为这三个周期的起使时间,d为开始时间,先整一个循环,总时间一直增加,循环条件是总时间减去起始时间在对其做对应周期的模运算,得到的余数如果都为零就代表三个周期的高峰同时出现了。但是交了代码之后发现超时了,超时代码如下:

TLE代码

import java.util.*;
public class Poj1006 {
 public static void main(String[] args){
  int p,e,i,d;
  Scanner keyboard=new Scanner(System.in);
  int t;
  int a=1;
  while(keyboard.hasNext()){
   p=keyboard.nextInt();
   e=keyboard.nextInt();
   i=keyboard.nextInt();
   d=keyboard.nextInt();
      if(p==-1&&e==-1&&i==-1&&d==-1)
       {break;}
   t=d;
   t++;
   while(!(((t-p)%23==0)&&((t-e)%28==0)&&((t-i)%33==0)&&(t<=21252)&&(t-p)>0&&(t-e)>0&&(t-i)>0))
    {
    t++;
    }
   System.out.println("Case "+a+": the next triple peak occurs in "+(t-d)+" days.");
   a++;
  }
 }
}

改进思路

通过查阅资料,发现老祖宗发明了一个叫孙子定理(中国剩余定理,Chinese Remainder theorem),这个定理厉害啊!哈哈哈。
先引用一个百度百科中的一个小故事:
《孙子算经》卷下第二十六题原文如下:
有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?
即,一个整数除以三余二,除以五余三,除以七余二,求这个整数。
标准解法:先从3和5、3和7、5和7的公倍数中相应地找出分别被7、5、3除均余1的较小数15、21、70 即:
15÷7=2……余1,
21÷5=4……余1,
70÷3=23……余1.
再用找到的三个较小数分别乘以所要求的数被7、5、3除所得的余数的积连加,
15×2+21×3+70×2=233. (将233处用i代替,用程序可以求出)
最后用和233除以3、5、7三个除数的最小公倍数.
233÷105=2……余23,
这个余数23就是合乎条件的最小数.
中国剩余定理不详细解释,下面的说明内容引用的百度词条。
中国剩余定理说明:假设整数m1,m2, … ,mn两两互质,则对任意的整数:a1,a2, … ,an,方程组
有解

分析

为什么要先找余数为1的较小的数呢?首先得知道乘数之余等于余数之积,加数之余等于余数之和。就是说可以先把这个整数分成若干个较小的整数,这几个较小的整数的和就是最终的整数。而这几个较小的整数跟所对应的除数又有何关系呢?那就是其中一个整数是三个数中的两个数的最小公倍数,且这个数除以第三个数所得的余数是题目中的余数。(这句话是个人理解,可能写的不太清楚,本人建议多查资料,多看看就理解了。)回到题中,题中所说的三个生理周期就是除数,p,e,i三个数就相当于对应的余数。余数是要求输入的,除数是固定的。所以可以根据一个公式直接求出最终的的整数,复杂度为O(1)。
通过标准解法来解题:设整数为n,n%23=p,n%28=e,n%33=i;先从23和28、23和33、28和33中的公倍数中找出分别被33、28、23除余数为1的公倍数。即:
1288%33=1
14421%28=1
5544%23=1
再用找到的三个较小数分别乘以所要求的数被33、28、23除所得的余数的积连加,
n=5544 * p+14421 * e+1288 * i
最后用n%lcm(23,28,23)即可得到这个整数,由于题中说明了也要输入d,开始的时间,所以最后要用的得到的整数减去这个开始的时间。

AC代码

import java.util.Scanner;
public class Poj1006ProCRT {
 public static void main(String[] args){
  int p,e,i,d;
  Scanner keyboard=new Scanner(System.in);
  int t;
  int a=1;
  while(keyboard.hasNext()){
   p=keyboard.nextInt();
   e=keyboard.nextInt();
   i=keyboard.nextInt();
   d=keyboard.nextInt();
      if(p==-1&&e==-1&&i==-1&&d==-1)
       {break;}
   t=(5544*p+14421*e+1288*i-d)%21252;
   if(t<=0)
    t=t+21252;
   System.out.println("Case "+a+": the next triple peak occurs in "+t+" days.");
   a++;
  }
 }
}

总结

这道题一开始做是超时的,超时之后第一想法的输入的方法写的太水了,改一改输入的方法应该可以ac,但是修改了输入的方法之后,还是超时,于是通过查阅资料发现了中国剩余定理这个定理,用这个定理就可以将时间复杂度为O(n)的循环算法直接转换成一个公式,其复杂度为O(1),可见效率的提升程度是巨大的。同时也看到了自己的不足,这道题也属于水题,但由于自己的才疏学浅,知道的太少了,而导致了这道题ac不出来,还需要努力,争取在这个假期把所有acm所覆盖的知识全部看一遍并懂得应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您的问题,我理解您想了解POJ1328问题的Java解决方案。POJ1328是一个经典的问题,也被称为"雷达安装"问题,它涉及到在一个二维平面上安装雷达以覆盖所有岛屿的最小雷达数量。以下是一个Java解决方案的示例代码: ```java import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int caseNum = 1; while (true) { int n = scanner.nextInt(); if (n == 0) { break; } Island[] islands = new Island[n]; for (int i = 0; i < n; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); islands[i] = new Island(x, y); } int radarNum = getRadarNum(islands); System.out.println("Case " + caseNum + ": " + radarNum); caseNum++; } } private static int getRadarNum(Island[] islands) { Arrays.sort(islands, Comparator.comparingInt(Island::getRight)); int radarNum = 1; int rightMost = islands[0].getRight(); for (int i = 1; i < islands.length; i++) { if (islands[i].getLeft() > rightMost) { radarNum++; rightMost = islands[i].getRight(); } else { rightMost = Math.min(rightMost, islands[i].getRight()); } } return radarNum; } static class Island { private int left; private int right; public Island(int left, int right) { this.left = left; this.right = right; } public int getLeft() { return left; } public int getRight() { return right; } } } ``` 这段代码通过输入岛屿的坐标,计算出需要安装的最小雷达数量,并输出结果。它使用了排序和贪心算法的思想来解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值