首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

每日一道算法_7_Biorhythms

2013-10-13 
每天一道算法_7_Biorhythms0 0 0 00 0 0 1005 20 34 3254 5 6 7283 102 23 320203 301 203 40-1 -1 -1 -1C

每天一道算法_7_Biorhythms

0 0 0 00 0 0 1005 20 34 3254 5 6 7283 102 23 320203 301 203 40-1 -1 -1 -1

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.

 

刚开始按照自己的思路,就是用了三个嵌套循环遍历所有可能出现的情况,但是三个周期总是没有相等的值,不知错在哪里,代码如下,希望大神研究研究:

 

import java.util.Scanner;public class Biorhythms {static int pp = 23;static int ee = 28;static int ii = 33;public static void main(String args[]) {Scanner in = new Scanner(System.in);int all = in.nextInt();int[] array = new int[all * 4];for (int i = 0; i < array.length; i++) {array[i] = in.nextInt();}int days = 0;for (int j = 0; j < all; j++) {int p = array[4 * j];int e = array[4 * j + 1];int i = array[4 * j + 2];int d = array[4 * j + 3];days = getDays(p, e, i, d);System.out.println("Case " + (j + 1)+ ": the next triple peak occurs in " + days + " days.");}}public static int getDays(int ps, int es, int is, int ds) {int y = 0;int p = ps;int e = es;int i = is;int d = ds;System.out.println(" p-->" + p + " e-->" + e + " i-->" + i);for (; p < 21252; p = p + pp) {for (; e < p; e = e + ee) {for (; i < e; i = i + ii) {System.out.println(" p-->" + p + " e-->" + e + " i-->" + i);if (p == e && p == i && e == i) {y = p - d;break;}}}}return y;}}


 

自己没做出来,就百度了一下资料,结果如下了,比较正规的解法:

这是一道典型的中国剩余定理(孙子定理)题,百科地址http://baike.baidu.com/link?url=vupDWKyEG0vb-4vi7PtvRsXV-wrWd0O8qg1Dx8SzlaFC6bt0fRi_0X9kkiXNWrFPJ5ID3kSE5K7WBuPFMBSaeei2PIFb4qYWv7PWn98A9xdR3c1oZJuYev_MJuwMipJ0BXOAGRdJCp8X5NjkfTGxhzRLU6iPdxwbrToG-gKBaNS

根据题意其实就是一个数被23整除余数是Lp=p % 23(p被23整除的余数),被28整除余数是Le=e % 28((e被28整除的余数),被33整除余数是Li=i % 33(i被33整除的余数),求这个数的最小正整数。这里之说以要p % 23而不是直接p是因为题目中提示并不是第一次到达最佳状态。

    做法:    首先算出能被23整除余1并且能被28和33同时整除的数,能被28整除余1并且能被23和33同时整除的数,能被33整除余1并且能被23和28同时整除的数。所以ei=28*33=924,pi=23*33=759,pe=23*28=644,i=23*28*33=21252;所以如果924*n % 23==1的话那么这个924*n就应该是我们在上一步中所要求的第一个数,其他三个数同理,记作P,E,I;因为余数是p,e,i并不是1所以最后都应该乘上去,结果就是(P*p+E*e+I*i) % 21252。    最后要对算出来的日期和题目所给的d进行比较,如果相等直接输出21252,如果比给的日期大,那么输出还必须减去d。如果比结果小必须输出21252-(d-结果); 下面是代码:

 

import java.util.Scanner;public class Main{public static void main(String[] args)throws Exception{Scanner cin=new Scanner(System.in);int m=1;while(cin.hasNext()){int p=0,e=0,i=0,d=0,lp=1,le=1,li=1,finalNum=0;p=cin.nextInt() % 23;e=cin.nextInt() % 28;i=cin.nextInt() % 33;d=cin.nextInt();if(p==-1||e==-1||i==-1||d==-1)break;while((924*lp) % 23!=1){lp++;}while((759*le) % 28!=1){le++;}while((644*li) % 33!=1){li++;}finalNum=(924*lp*p+759*le*e+644*li*i) % 21252;System.out.println(finalNum);if(finalNum==d){finalNum=21252;}else if(finalNum<d){finalNum=21252-(d-finalNum);}else if(finalNum>d){finalNum=finalNum-d;}System.out.println("Case "+m+": the next triple peak occurs in "+finalNum+" days.");m++;}}}


 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)

 

 

热点排行