a263.
日期差幾天
--
板橋高中練習題
| From: [120.119.33.43] |
發表日期
:
2019-12-14 21:24
import java.util.Scanner;
public class a263 {
public static void main(String[] argv) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
judge sum1 = new judge();
judge sum2 = new judge();
while(sc.hasNext()) {
int year = sc.nextInt();
int month = sc.nextInt();
int date = sc.nextInt();
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int date2= sc.nextInt();
int d1=sum1.sum(year, month, date);
int d2=sum1.sum(year2, month2, date2);
int total = Math.abs(sum1.sum(year, month, date)-sum2.sum(year2,month2,date2));
System.out.println(total);
}
}
}
class judge{
public boolean yearjudge(int year) { //判斷是否為閏年 回傳1或0
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
return true;
}
else{
return false;
}
}
public int sum(int year,int month ,int date) { //給數值
int []md ={0,31,28,31,30,31,30,31,31,30,31,30,31}; //平年日期
int []md1 ={0,31,29,31,30,31,30,31,31,30,31,30,31}; //閏年日期
int sum =0; //加總初始值為0
for(int i =1 ; i < year ;i++) { //一年一年跑起來~
if(yearjudge(i)==true) { //判斷閏年
sum+=366; //是的話+36
}else {
sum+=365; //不是的話+365
}
}
if(yearjudge(year)==true) { //判斷是不是閏年
for(int i=1 ;i<month ;i++ ) { //是的話使用閏年陣列
sum+=md1[i];
}
}else {
for(int i=1 ;i<month ;i++ ) { //不是的話使用平年陣列
sum+=md[i];
}
}
sum+=date-1;
return sum; //最後加總扣掉自己
}
}