One, test instructions: Chinese in upper right corner.
Second, the idea:
1, the equations are derived from test instructions
2, using the Chinese remainder theorem to solve
3, find the smallest positive integer
Three, step:
1, the equations are derived from test instructions
(n+d)% (p);
(n+d)% = e;
(n+d)% = i;
2, China remainder theorem solution
I, from the common multiple of 23 and 28 to find X, and meet x%33 = 1, x=1288
II, find y from the common multiple of 23 and 33, and meet y%28 = 1, y=14421
III, find z from the common multiple of 28 and 33, and meet z%23 = 1, z=5544
Iiii,s = (n+d) = (5544*p+14421*e+1288*i)% LCM (23,28,33); LCM for least common multiple = 21252;
3, finding the minimum positive integer solution
I, prevent n is negative: n= (n+21252)%21252
Note that when n=0, the result is output 21252.
1#include <iostream>2 using namespacestd;3 4 intMain () {5 intP,e,i,d,n,s,l=1;6 while(cin>>p>>e>>i>>d) {7 if(p==-1&&e==-1&&i==-1&&d==-1) Break;8 intN= (5544*p+14421*e+1288*i)%21252-D;9N= (n+21252)%21252;//prevent S from being negativeTen if(n==0) Onen=21252; Aprintf"Case %d:the Next triple peak occurs in%d days.\n", l++, n); - } - return 0; the}
View Code
1006-biorhythms (Chinese remainder theorem)