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.
簡單的來說,就是求一個最小的數X。使得 X % 23 = p; X % 28 = e; X % 33 = i;
這就是著名的中國剩餘定理。
數學上的常規解法是這樣的:
找到一個找到一個自然數t,使得 (e*i) * t % 23 = p . 這裡的t要從1開始試。 令 a = (e*i) * t
再依次找到 b,c。 a+b+c則符合條件。
編程中,有一個更簡便的方法。
找到一個數使得X , 使得 X % (e * i) = 1; (可使用上面的方法一個一個試)。
則 X * p % (e * i) = p;
代碼如下:
5544 % 23 = 1; 5544 % (28 * 33) = 0;
#include <stdio.h>int main(){int p, e, i, n, sum, t;t = 0;while (scanf("%d%d%d%d", &p, &e, &i, &n), p + e + i + n != -4){sum = (5544 * p + 14421 * e + 1288 * i) % 21252;if (sum - n <= 0)sum += 21252;printf("Case %d: the next triple peak occurs in %d days.\n", ++t,sum - n);}return 0;}