題目描述
人生來就有三個生理週期,分別爲體力、感情和智力週期,它們的週期長度爲23 天、 28 天和33 天。每一個週期中有一天是高峯。在高峯這天,人會在相應的方面表現出色。例如,智力週期的高峯,人會思維敏捷,精力容易高度集中。因爲三個週期的周長不同,所以通常三個週期的高峯不會落在同一天。對於每個人,我們想知道何時三個高峯落在同一天。對於每個週期,我們會給出從當前年份的第一天開始,到出現高峯的天數(不一定是第一次高峯出現的時間)。你的任務是給定一個從當年第一天開始數的天數,輸出從給定時間開始(不包括給定時間)下一次三個高峯落在同一天的時間(距給定時間的天數)。例如:給定時間爲10,下次出現三個高峯同天的時間是12,則輸出2(注意這裏不是3)。
輸入數據
輸入包含多組數據,每一組數據由四個整數組成,數據以-1 -1 -1 -1 結束。對於四個整數p, e, i 和d。 p, e, i 分別表示體力、情感和智力高峯出現的時間(時間從當年的第一天開始計算)。d 是給定的時間,可能小於p, e, 或 i。 所有給定時間是非負的並且小於365, 所求的時間小於等於21252。
輸出要求
從給定時間起,下一次三個高峯同天的時間(距離給定時間的天數)。
輸入樣例
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.
一看到這題目的時候,我就想到用for循環一個一個遍歷,因爲數據量不大。哈哈
#include<iostream>
using namespace std;
int main()
{
int p,e,i,d;
int count=1;
while(1){
cin>>p>>e>>i>>d;
if(p != -1){
for(int j=d+1;j<=21252;j++){
if((j-p) % 23 == 0 && (j-e) % 28 == 0 && (j-i) % 33 == 0)
cout<<" Case "<<count++
<<" : the next triple peak occurs in "<<j-d
<<" days."<<endl;
}
}
else
break;
}
return 0;
}
輸出:
枚舉算法實現:
#include<iostream>
using namespace std;
int main()
{
int p,e,i,d,j;
int count=1;
while(1){
cin>>p>>e>>i>>d;
if(p != -1){
for(j=d+1;j<21252;j++)
if((j-p) % 23 == 0)
break;
for(;j<21252;j+=23)
if((j-e) % 28 == 0)
break;
for(;j<21252;j+=23*28)
if((j-i) % 33 == 0)
break;
}
else
break;
cout<<" Case "<<count++
<<" : the next triple peak occurs in "<<j-d<<" days."<<endl;
}
return 0;
}