POJ 1006 Biorhythms

Source: Internet
Author: User

Links: http://poj.org/problem?id=1006

Biorhythms
Time limit:1000ms Memory limit:10000ktotal submissions:121231 accepted:38168DescriptionSome people believe that there be three cycles in a person's life that's start the day he or she's 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.

InputYou'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.

Outputfor 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.


Sample Input0 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

Sample OutputCase 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.

Source
East Central North America 1999

The main idea-human beings are born with three cycles of physical, emotional and intellectual cycles, with periods of 23 days, 28 days and 33 days. For everyone, we want to know when three peaks fall on the same day. For each cycle, we will give the number of days from the first day of the current year to the peak (not necessarily the time of the first peak). Q: Given a number of days from the first day of the year, the output starts at a given time (excluding a given time) and the next three peaks fall on the same date (the number of days from a given time).

Idea-because all the given time is non-negative and less than 365, the time to ask is less than 21252. So just enumerate the days from 1 until the conditions are met.

Complexity analysis--time complexity: O (n), spatial complexity: O (1)

Attach the AC code:


#include <iostream> #include <cstdio> #include <iomanip> #include <string> #include <cstring > #include <cmath> #include <algorithm>using namespace std;const double PI = ACOs ( -1.0); const int LOOP1 = 23; const int LOOP2 = 28;const int loop3 = 33;int Main () {    ios::sync_with_stdio (false);    int P, E, I, D;    int cas = 1;    while (CIN >> p >> e >> i >> D)    {        if (p==-1 && e==-1 && i==-1 && D ==-1) break            ;        int day = 1;        while (day)        {            if (day-p)%loop1==0 && (day-e)%loop2==0 && (day-i)%loop3==0 && day> d) break                ;            day++;        }        Day-= D;        cout << case << cas << ': The next triple peak occurs in ' << day << ' days.\n ';        cas++;    }    return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1006 Biorhythms

Related Keywords:

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.