POJ-1006 Biorhythms Cycle meet two Ideas program

Source: Internet
Author: User
Tags min

Description Some people believe that there was three cycles in a person's life that start the day he or she was born. These three cycles is the physical, emotional, and intellectual cycles, and they have periods of lengths, D, and 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 has 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'll be given the number of days from the beginning of the current year at which one of its peaks (no T necessarily the first) occurs. You'll also be given a date expressed as the number of days from the beginning of the current year. You task was 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 ten and the next triple peak occurs on day, the answer was 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'll be given a number of cases. The input for each case consists of a line of four integers p, E, I, and D. The values P, E, and I are is the number of days from the beginning of the which the physical, emotional, and Intellectual cycles peak, respectively. The value D is the given date and could be smaller than any of P, E, or I. All values is non-negative and at most 365, and your may assume that a triple peak would occur within 21252 days of the GIV En 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 PE AK, 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
5 325
4 5 6 7
283 102
203 301 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 a problem:

1 first calculate the day of the two-cycle encounter and then verify that the day meets the third cycle

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

Idea 1 of the program:

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 days when I and E meet

The second Whlle cycle is determined that I and E must meet the day is I, i+924, i+924*2 ... And then verify that these days are also met with 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);//Pay attention to the formula calculation do not make a mistake
	return p-d;
}

Train of thought 2 seems simpler, better understanding points.

Finally the 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 is slow, hehe, the computational complexity of the time is the same, OJ system calculation of the running time estimate is not completely reliable bar.

Application of two ideas:

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

2 but if, as the case, the three speed is similar, then the use of addition, do not mold operation, perhaps the second reason is fast.


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.