題意:中國餘數定理的裸題,
INPUT=>a b c d
OUTPUT=> ans
其中 (ans+d)%23==a,(ans+d)%28==b,,(ans+d)%33==c
胡言亂語:因此可以先求出(ans+d)在-d就是答案了!
這題簡單來說就是中國餘數定理(點我),帶入公式就是答案的 =)
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<cstdio> | |
#include<cmath> | |
#include<cstdlib> | |
using namespace std; | |
int GCD(int p,int q){ | |
while(p%=q) | |
swap(p,q); | |
return q; | |
} | |
int LCM(int a, int b) { | |
if(a<b) swap(a,b); | |
return a*b/GCD(a, b); | |
} | |
int test(int K2, int K) { | |
int temp = K2; | |
while (true) { | |
if (K2 % K == 1) break; | |
else | |
K2 += temp; | |
} | |
return K2; | |
} | |
int main(){ | |
int Biorhythms = 23*28*33 ,k = 0,a ,b, c ,d,ans; | |
int aa = test(28*33,23); | |
int cc = test(23*28,33); | |
int bb = test(23*33,28); | |
while(scanf("%d %d %d %d",&a,&b,&c,&d)==4){ | |
k++; | |
if(a==-1 and b==-1 and c==-1 and d==-1) break; | |
ans = (aa*a+bb*b+cc*c-d+Biorhythms)%Biorhythms; | |
if(!ans) ans = Biorhythms; | |
printf("Case %d: the next triple peak occurs in %d days.\n",k,ans); | |
} | |
} |
有點想睡...但好像原本要讀計組的說@@
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。