import java.util.Scanner;
public class a263 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int []md ={31,28,31,30,31,30,31,31,30,31,30,31};
int year = sc.nextInt();
int month = sc.nextInt();
int date = sc.nextInt();
int judge=0;
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int date2= sc.nextInt();
int judge2=0;
for(int i = 0 ; i <= year ; i++) {
if(year%4==0 && year/100!=0 || year%400==0) {
judge++;
}
}
year*=365;
month=0;
for(int i = 1 ; i <= md.length ; i++) {
month+=md[i-1];
}
long total = year+month+date+judge;
for(int i = 0 ; i <= year2 ; i++) {
if(year2%4==0 && year2/100!=0 || year2%400==0) {
judge2++;
}
}
year2*=365;
month2=0;
for(int i = 1 ; i <= md.length ; i++) {
month2+=md[i-1];
}
long total2 = year2+month2+date2+judge2;
System.out.println(Math.abs(total-total2));
}
}
}
import java.util.Scanner;
public class a263 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int []md ={31,28,31,30,31,30,31,31,30,31,30,31};
int year = sc.nextInt();
int month = sc.nextInt();
int date = sc.nextInt();
int judge=0;
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int date2= sc.nextInt();
int judge2=0;
for(int i = 0 ; i <= year ; i++) {
if(year%4==0 && year/100!=0 || year%400==0) {
judge++;
}
}
year*=365;
month=0;
for(int i = 1 ; i <= md.length ; i++) {
month+=md[i-1];
}
long total = year+month+date+judge;
for(int i = 0 ; i <= year2 ; i++) {
if(year2%4==0 && year2/100!=0 || year2%400==0) {
judge2++;
}
}
year2*=365;
month2=0;
for(int i = 1 ; i <= md.length ; i++) {
month2+=md[i-1];
}
long total2 = year2+month2+date2+judge2;
System.out.println(Math.abs(total-total2));
}
}
}
更.....
您的答案為: 227032
正確答案為: 227031
答案差了一天 試問各位大大 是哪裡的問題呢 看了老半天看不出來
您這寫法應該會超時。
可以查一下 datediff 來驗算。
程式碼 我改成這樣 一樣有超時問題
import java.util.Scanner;
public class a263 {
public static void main(String[] argv) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int []md ={0,31,28,31,30,31,30,31,31,30,31,30,31};
int year = sc.nextInt();
int month = sc.nextInt();
int date = sc.nextInt();
int judge=0;
int year2 = sc.nextInt();
int month2 = sc.nextInt();
int date2= sc.nextInt();
int judge2=0;
for(int i = 1 ; i < year ; i++) {
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0 && month>2 ) {
++judge;
}else if(year2 % 4 == 0 && year2 % 100 != 0 || year2 % 400 == 0 && month>2) {
++judge2; //判斷到一次閏年+1
}
}
for(int i = 1 ; i < year ; i++) {
year+=365;
year2+=365;
}
month=0;
month2=0;
for(int i = 1 ; i < md.length ; i++) {
month+=md[i-1];
month2+=md[i-1];
}
int total = (year + month + date +judge)-(year2 + month2 + date2 +judge2);
System.out.println(Math.abs(total));
}
}
}