ACM题 Biorhythms 将中国剩余定理的思想包含在程序中

来源:互联网 发布:java工程师要多少钱 编辑:程序博客网 时间:2024/04/27 14:37


这个是我结合别人的代码与解题的思想而写出的代码,代码中的所有数据都变的有理有据。 可以先看大家对这道题的解法,对于不懂的数据来源再看我的代码

下面是题目要求:

题目描述
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.
输入
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.
输出
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.
样例输入
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
样例输出
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.

 

代码:

 

#include <stdio.h>int nums[3]={23,28,33}; //这三个数必须互质  于是利用 中国剩余定理(韩信点兵) int mulProduct;//这个是记录这三个互质数的最小公倍数int makeModOne[3];//记录使两个数相乘 再乘一个数 除以另一个数 余 1的数 (最小) int modOne[3];//记录使两个数相乘除以另一个数可以余 1的数  (最小) void init(){    mulProduct=nums[0]*nums[1]*nums[2];    int i;    for(i=1;(i*nums[2]*nums[1])%nums[0]!=1;i++);    makeModOne[0]=i;    modOne[0]=i* nums[2]*nums[1];    for(i=1;(i*nums[0]*nums[2])%nums[1]!=1;i++);    makeModOne[1]=i;    modOne[1]=i* nums[2]*nums[0];    for(i=1;(i*nums[0]*nums[1])%nums[2]!=1;i++);    makeModOne[2]=i;    modOne[2]=i* nums[0]*nums[1];}    int main() { int p,e,i,d,n; int num=1; init();while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF){     if(p!=-1)     {         /*完整  但是其实也没必要         p%= nums[0];e%= nums[0];i%= nums[0];        //下面为一个理解的代码         (5544×p+14421×e+1288×i)%(23×28×33)=n+d         n=(5544×p+14421×e+1288×i-d)%(23×28×33)           */         n=(modOne[0]*p+modOne[1]*e+modOne[2]*i - d + mulProduct ) % mulProduct;         if(n==0)n=mulProduct;        printf("Case %d: the next triple peak occurs in %d days.\n",num++,n);    }     else break; } return 0;}