自己写了一个暴力的,然后找了一个还包含典故的算法。中国文化博大精深。。(转自:http://blog.youkuaiyun.com/lyy289065406/article/details/6648551)
#include <iostream>
using namespace std;
void p1006()
{
//first violate:
/*const int A = 23, B = 28, C = 33;
int a,b,c,i;
int past,index = 0;
while(cin>>a>>b>>c>>past, a>=0 && b>=0 && c>=0 && past>=0)
{
index ++;
for(i = past + 1; ; i++)
{
if((i-a)%A == 0 && (i-b)% B ==0 && (i-c)% C == 0)
{
cout<<"Case "<<index<<": the next triple peak occurs in "<<i - past<<" days."<<endl;
break;
}
}
}*/
//second HanXin's method
const int A = 23, B = 28, C = 33, x = 5544, y = 14421, z = 1288;//x = 8 * (28 * 33), x % 23 = 1; y = 8 * (23 * 33), y % 28 = 1; z = 2 * (23 * 28), z % 33 = 1;
int a,b,c,n,past,index = 0;
while(cin>>a>>b>>c>>past, a>=0 && b>=0 && c>=0 && past>=0)
{
index ++;
n = (x * a + y * b + z * c - past + 21252) % 21252;
if(n == 0) n = 21252;
cout<<"Case "<<index<<": the next triple peak occurs in "<<n <<" days."<<endl;
}
}