從西元年較小的開始算
寫一個判斷閏年的function
switch 蠻好用的
code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class main {
public static void main(String[] args) throws IOException {
/*AC (0.3s, 7.6MB)*/
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String first;
while ((first = br.readLine()) != null) {
String second = br.readLine();
String[] f = first.split(" ");
String[] s = second.split(" ");
int y1 = Integer.parseInt(f[0]);
int m1 = Integer.parseInt(f[1]);
int d1 = Integer.parseInt(f[2]);
int y2 = Integer.parseInt(s[0]);
int m2 = Integer.parseInt(s[1]);
int d2 = Integer.parseInt(s[2]);
int start = Math.min(y1, y2);
int during = Math.abs(fuckday(y1, m1, d1, start) - fuckday(y2, m2, d2, start));
System.out.println(during);
}
}
static int fuckday(int y, int m, int d, int start) {
int sum = 0;
for (int i = start; i < y; i++) {
if (leap(i)) {
sum += 366;
} else {
sum += 365;
}
}
for (int i = 1; i < m; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
break;
case 4:
case 6:
case 9:
case 11:
sum += 30;
break;
default:
if (leap(y))
sum += 29;
else
sum += 28;
}
}
sum += d;
return sum;
}
static boolean leap(int y) {
return ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) ? true : false;
}
}