人生来就有三个生理周期,分别为体力周期、感情周期和智力周期,它们的周期长度分别为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,在智力周期的高峰,人会思维敏捷,注意力容易高度集中。因为三个周期的长度不同,所以通常三个周期的高峰不会落在同一天。对于每个人,想知道何时三个高峰落在同一天。对于每个周期,会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。给定一个从当年第一天开始的天数,你的任务是输出从给定时间开始(不包括给定时间),下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同一天的时间是12,则输出2(注意这里不是3)。
input
输入包含多组数据,每一组数据由四个整数组成,数据以-1 -1 -1 -1 结束。
对于四个整数p, e, i和d,p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d是给定的时间,可能小于p, e或i。所有给定时间是非负的并且小于或等于365,所求的时间小于或等于21252。
output
从给定时间起,下一次三个高峰同一天的时间(距离给定时间的天数)。
sample input
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
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
做本题时要注意枚举的目的就不会在循环里面在写循环了,只要满足条件即完成了一次循环的目的,如找到23 的倍数 23*28 的倍数
做法
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int time=0;int a,b,c,d;
while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){
if(a==-1&&b==-1&&c==-1&&d==-1) break;
time++;
//确定循环遍历的初始值
int n1=d/23 ;
int n2=d/28;
int n3=d/33;
a=a%23+n1*23;b=b%28+n2*28;c=c%33+n3*33;
while(true){
if(a>21252){
cout<<"fail to find"<<endl;
break;
}
if(a==b&&b==c&&a>d){
printf("Case:%d,the next triple peak occurs in %d days.\n",time,a);
break;
}
a+=23;b+=28;c+=33;//尝试优化步长
}
}
}
正解
#include<iostream>
#include<stdio.h>
using namespace std;
int main(){
int t=0;int a,b,c,d;
while(true){
scanf("%d%d%d%d",&a,&b,&c,&d);
t++;
if(a==-1) break;int j=0;
for( j=d+1;j<21252;j++){
if((j-a)%23==0) break;//+23确保是23的倍数 进一步考虑28的倍数
}
for(;j<21252;j+=23){
if((j-b)%28==0) break;//满足条件的执行下一步 until 跳出循环
}
for(;j<21252;j+=23*28){
if((j-c)%33==0) break;
}
printf("Case:%d:the next triple peak occurs in %d days.\n",t,j-d);//最坏情况下输出21252
}
}