0%

C++ 106 花蓮 一起回家的日子

題目來源:106東區學科能力競賽

gcd轉lcm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
using namespace std;

int dayInM[12]={31,28,31,30,31,30,31,31,30,31,30,31};

void check(int y){
if((y%4==0 && y%100!=0) || y%400==0){
dayInM[1]=29;
}else{
dayInM[1]=28;
}
return;
}

int gcd(int a,int b){
if(a%b==0){
return b;
}else{
return gcd(b,a%b);
}
}

int main(){
int n;
scanf("%d",&n);

int dt[500];
for(int i=0;i<n;i++){
scanf("%d",dt+i);
}

int y,m,d;
scanf("%d/%d/%d",&y,&m,&d);
for(int i=0;i<n-1;i++){
int d=gcd(dt[i],dt[i+1]);
int min=d*dt[i]/d*dt[i+1]/d;
dt[i+1]=min;
}

d+=dt[n-1];

check(y);
while(d>dayInM[m-1]){
d-=dayInM[m-1];
m++;
if(m>12){
m=1;
y++;
}
check(y);
}
printf("%4d/%02d/%02d",y,m,d);
return 0;
}