poj:1006 Biorhythms Cycle meet two Ideas program

Source: Internet
Author: User
Tags min

Description

Some people believe that there are three cycles into a person's life, which is the start of the day, he, or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods to lengths, D Ays, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at the corresponding field (physical, emotional, or mental). For example, if it is the mental curve, thought processes would be sharper and concentration would be easier.
Since the three cycles have different periods, the peaks of the three cycles, generally occur at different. We would like to determine when a triple peak occurs (the peaks of all three cycles occur on the same day) is for any person. For each cycle, you are given the number of days from the beginning of the "Current year" at which one of its peaks (no T necessarily the occurs. You'll also be given a date expressed as the number of "the" and "the" of "the" of "the". You are to determine the number of the given date to the next triple peak. The given date is not counted. For example, if the given date is ten and the next triple peak occurs on day, the answer is 2, not 3. The 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 are 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 "the", at which the physical, emotional, and Intellectual cycles peak, respectively. The value D is the given date and could smaller than any of P, E, or I. All values are non-negative and at most 365, and I assume that a triple peak'll occur within 21252 days of the GIV En date. The end of the input was 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 the next triple peak, in The form:

Case 1:the Next triple peak occurs in 1234 days.

Use the "plural" form ' even if ' the answer is 1.

Sample Input

0 0 0 0
0 0 0 5
325 4 5 6 7
283 102
203 203
-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.

The actual problem is the same as the hour hand, the minute hand and the second hand of my previous blog.

There are two ways to solve problems:

1 Calculate the date of the two-cycle encounter and verify that the date meets the third cycle.

2 three cycles as three runners, run to a location (date) Stop, let the slowest first run, see if three meet, and then let the slowest first run, stop, verify ... So loop

The program of Idea 1:

int runnersmeet (int p, int e, int i, int d)  
{  
    P%=, e%=, I%=;    
    while ((I-E)%) i + =;  
    while ((i-p)% i = = 924;//28*33 = = 924  
    if (i <= D) return 21252-(d-i);  
    return i-d;  

}

The first while loop is to calculate the day when I and E meet.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The second Whlle cycle is to determine that I and E must meet the day is I, i+924, i+924*2 ... And then verify that these days are also meeting p.

Idea 2 Program:

int runnersmeet (int p, int e, int i, int d)  
{  
    P%=, e%=, I%=;    
    while (!) ( p = = e && e = i))  
    {  
        int m = min (p, min (e, i))  
        ; if (m = = p) p + =;  
        else if (m = = e) E = =;  
        else i = +;  
    }  
    if (P <= D) return 21252-(d-p);//note formula calculation don't get me wrong return  
    p-d;  
}

Idea 2 seems simpler, better understanding points.

The final main program, on AC.

void Biorhythms ()  
{  
    int p,e,i,d, c = 0;  
    while (cin>>p>>e>>i>>d && 1!= d)  
    {  
        C + +;  
        printf ("Case%d:the next triple peak occurs in%d days.\n",  
            C, Runnersmeet (P, E, I, D));  
    }  
     
int main ()  
{  
    biorhythms ();  
    return 0;  
}

Feeling is the first idea of the program to go faster, but the actual operation is the first program slow, oh, the calculation of the time complexity is the same, OJ system calculation of the running time is not completely reliable.

Application of two ideas:

1 if the speed of one and the other two of the speed difference is very large, such as the hour hand, minute and second hand, the second hand to go a lot faster, then it must be the first train of thought fast,

2 but if, as the case may be, the speed of the three is almost the same, then the use of addition, not mode operation, perhaps is the second reason for quick thinking.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.