所谓的“中国剩余定理”,源于韩信点兵的故事,这种算法网上一搜一大把,我也就不赘述了。说实话这题不看网上的一些算法说明,还真是不好解决。
代码如下:
/*
Arthur: Near_zh
Date: 2014/08/03
Note: Poj1006
*/
#include <iostream>
#include <cmath>
using namespace std;
#define LCM 21252
#define A 5544
#define B 14421
#define C 1288
int main()
{
int p;//体力周期23
int e;//情感周期28
int i;//智力周期33
int d;//给定时间
int result;
int n=0;
while(cin>>p>>e>>i>>d)
{
if(p==-1&&e==-1&&i==-1&&d==-1)
break;
n++;
result=(A*p+B*e+C*i-d)%LCM;
if(result<=0)
result+=LCM;
//cout <<"result="<<result<<endl;
cout<<"Case "<<n<<": the next triple peak occurs in "<<result<<" days."<<endl;
}
}