The problem of physiological cycle enumeration algorithm

Source: Internet
Author: User

Take advantage of the winter vacation to learn C + + .....

The physiological cycle problem is a relatively simple algorithm problem, applied to the idea of enumeration.

Humans are born with three cycles of physical, emotional, and intellectual cycles, each of which lasts 23 days,

28 days and 33 days. One day in each cycle is a peak. At the peak of the day, people will do well in the corresponding aspects. Cases

For example, the peak of the intellectual cycle, people will be quick thinking, energy is easy to highly concentrated. Because the circumference of the three cycles is different,

Usually the peak of three cycles does not fall on the same day. For everyone, we want to know when three peaks fall on the same day.

For each cycle, we give the number of days from the first day of the current year to the peak (not necessarily the first

Time of peak appearance). Your task is to give a number of days from the first day of the year, and the output starts at a given time

(excluding a given time) the next three peaks fall on the same day (the number of days from a given time). For example: given

The time is 10, the next occurrence three peaks the same day time is 12, then the output 2 (note this is not 3).

Input data

Enter four integers: p, E, I, and D. P, E, I, respectively, indicate the time at which the physical, emotional and intellectual peaks appear (time

Calculated from the first day of the year). D is the given time, which may be less than P, E, or I. All given times are non-negative

and less than 365, the desired time is less than or equal to 21252.

Output requirements

From a given time, the next three peaks of the same day (the number of days from a given time).

Input sample

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

Output sample

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.

Thinking of solving problems

Assuming that the number begins on the first day of the year, three peaks occur simultaneously on page x. The x required to meet the problem needs to be greater than

D, less than or equal to 21252, and meet the following three conditions:

1) (x-p)% 23 = 0

2) (X-E)% 28 = 0

3) (x-i)% 33 = 0

And if you simply think of enumerating from d+1 days, the complexity is too high and inefficient.

Think of what we are looking for is the three highest period of the same day, so, take a jump-look. We only look for the highest intelligence period, and then from the highest period of intelligence to find the highest emotional period, and then from the highest period of the two to find the highest physical strength.

This greatly saves time.

The C + + code is as follows:

#include <iostream>
#include <cstdio>

using namespace Std;

#define N 21252

int main ()
{
int p,e,i,d,n,k;
n=0;
while (CIN >> p >> e >> i >> d && p!=-1)
{
n++;
for (k=d+1; (k-p)%23;k++);
for (;(k-e)%28;k+=23);
for (;(k-i)%33;k+=23*28);
cout << "Case" << n << ": The next triple peak occurs in" << k-d << Endl;
}
return 0;
}

The problem of physiological cycle enumeration algorithm

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.