You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date.
Case: the next triple peak occurs in 1234 days. Use the plural form "days'' even if the answer is 1.
0 0 0 0
Case: the next triple peak occurs in 21252 days.
#include <stdio.h> void TimeGoesBy(int d[4], int len[4]) { if(d[1]<=d[2]&&d[1]<=d[3]) d[1]+=len[1]; else if(d[2]<=d[1]&&d[2]<=d[3]) d[2]+=len[2]; else d[3]+=len[3]; } int main() { int d[4];//分别代表今天日期、1峰日期、2峰日期、3峰日期 int len[4]={0, 23, 28, 33}; while(scanf("%d%d%d%d",&d[1],&d[2],&d[3],&d[0])!=EOF) { for(int i=1; i<=3; i++) { while(d[i]>d[0]) d[i]-=len[i]; } TimeGoesBy(d, len); while(d[1]!=d[2] || d[1]!=d[3]) TimeGoesBy(d, len); printf("Case: the next triple peak occurs"); printf(" in %d days.\n", d[1]-d[0]); } return 0; }
记录三个峰值的日期,每次循环都选择当前日期最小的一个加上周期,直到三者相等
#include<iostream>
using namespace std;
int main(){
int p, e, i, d;
while(cin >> p >> e >> i >> d){
int ps = p, es = e, is = i;
while(!(ps == es && ps == is) || ps - d == 0){
if(ps <= es && ps <= is)
ps += 23;
else if(es <= ps && es <= is)
es += 28;
else if(is <= ps && is <= es)
is += 33;
}
cout << "Case: the next triple peak occurs in " << ps - d << " days." << endl;
}
return 0;
}
try: while True: p,e,i,d = list(map(int,input().split())) a = b = c = 0 if p < e: a = 1 else: b = 1 while not (23*a+p == 28*b+e == 33*c+i): #谁小就加谁,直到相等 if 23*a+p == min(23*a+p, 28*b+e, 33*c+i): a += 1 elif 28*b+e == min(23*a+p, 28*b+e, 33*c+i): b += 1 else: c += 1 print('Case: the next triple peak occurs in %d days.' % (28*b+e-d)) except Exception: pass
def shizidinli(a, b, c, d): for i in range(1, 21253): k = d + i if (k-a) % 23 == 0 and (k-b) % 28 == 0 and (k-c) % 33 == 0: print('Case: the next triple peak occurs in {} days.'.format(i)) break # while True: # try: s = list(map(int, input().split())) shizidinli(s[0], s[1], s[2], s[3]) # except: # break
#include <iostream> using namespace std; //p e i分别为三个周期的起始日期 int main(){ int p, e, i, start; while (cin >> p >> e >> i >> start){ int final = start+1; //不能是同一天 //只要不满足三个周期都能整除就循环+1 while (!((final-p)%23==0 && (final-e)%28==0 && (final-i)%33==0)){ final++; } cout << "Case: the next triple peak occurs in " << final-start << " days." <<endl; } }
#include <cstdio> #include <iostream> using namespace std; int main() { int p, e, i, d; while (cin >> p >> e >> i >> d) { while (!(p == e && p == i && p > d)) { if (p <= e && p <= i) p += 23; else if (e <= p && e <= i) e += 28; else i += 33; } printf("Case: the next triple peak occurs in %d days.\n", p - d); } }
#include <cstdio> #include <iostream> using namespace std; int arr[3]={23,28,33}; //返回最小天数的序号 int mini(int iarr[]){ if(iarr[0]<=iarr[1]&&iarr[0]<=iarr[2]){ return 0; }else if(iarr[1]<iarr[0]&&iarr[1]<iarr[2]){ return 1; }else{ return 2; } } int main() { int iarr[3];int id; while(scanf("%d%d%d%d",&iarr[0],&iarr[1],&iarr[2],&id)!=-1){ //全都相等且比给定天数大才跳出循环 while(!(iarr[0]==iarr[1]&&iarr[1]==iarr[2]&&iarr[0]>id)){ //哪个小就加哪个,直到相等 int i=mini(iarr); iarr[i]+=arr[i]; } printf("Case: the next triple peak occurs in %d days.\n",iarr[0]-id); } }
#include <iostream> #include <algorithm> using namespace std; int main(){ int p, e, i, d; int pp=23, pe=28, pi=33; while(cin>>p>>e>>i>>d){ int j=max(p, max(e, i)), day=1; while(1){ j++; if((j-p)%pp==0&&(j-e)%pe==0&&(j-i)%pi==0&&j>d){ cout << "Case: the next triple peak occurs in " << j-d << " days." << endl; break; } } } return 0; }
#include<iostream> using namespace std; int p,e,i,d; int main() { while (cin >> p >> e >> i >> d) { int m = (5544*p+14421*e+1288*i) % 21252; if (m <= d) { m += 21252; } cout << "Case: the next triple peak occurs in " << m-d << " days." << endl; } return 0; }孙子定理的解法,可以避开枚举直接得到答案。
/*采用3个map映射存储所有的可行解,然后直接遍历一个Map, 判断其余的map是否有与其匹配的元素*/ #include <iostream> #include <map> using namespace std; int cp=23,ce=28,ci=33; map<int,bool> MapP,MapE,MapI; int main() { int p,e,i,d; while(cin>>p>>e>>i>>d){ int start = p+cp;//从下一个峰值开始计算 while(start<=21252){ MapP[start]=true; start+=cp; } start = e+ce; while(start<=21252){ MapE[start]=true; start+=ce; } start = i+ci; while(start<=21252){ MapI[start]=true; start+=ci; } for(auto it=MapP.begin();it!=MapP.end();it++){ if(MapE[it->first]&&MapI[it->first]){ printf("Case: the next triple peak occurs in %d days.\n",it->first-d); break; } } } return 0; }
#include int main(){ const int a=23,b=28,c=33; int p,e,i,d,M,M1,M2,M3,m1,m2,m3,x; M = a*b*c; M1 = M/a; M2 = M/b; M3 = M/c; while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF){ int t1=M1%a,t2=M2%b,t3=M3%c; m1=m2=m3=0; while(t1*m1%a != 1) m1++; while(t2*m2%b != 1) m2++; while(t3*m3%c != 1) m3++; x = p*M1*m1 + e*M2*m2 + i*M3*m3; if(p==e && p==i && i==e) printf("Case: the next triple peak occurs in %d days.",d<=p?(p-d):M-(d-p)); else printf("Case: the next triple peak occurs in %d days.",(x-d)%M);//求出最近的符合条件的天数 } return 0; }
#!/usr/bin/python #-*- coding:utf-8 -*- def triplePeak(p, e, i, d): maxLimit = (d+21252) flag = [0]*(maxLimit+1) rlt = maxLimit p = p%23 if p%23 else 23; e = e%28 if e%28 else 28; i = i%33 if i%33 else 33 while p <= maxLimit: flag[p] += 1 p += 23 while e <= maxLimit: flag[e] += 1 e += 28 while i <= maxLimit: if flag[i]==2 and i>d: rlt = i break i += 33 return (rlt-d) idx = 1 while True: try: p, e, i, d = map(int, raw_input().strip().split()) if p==-1 and e==-1 and i==-1 and d==-1: break print 'Case %d: the next triple peak occurs in %d days.' % (idx, triplePeak(p, e, i, d)) idx += 1 except: break