2016年1月15日 星期五

d532: 文文的求婚 (三)

內容 :
經過了一番苦練之後,文文終於可以很流利地用英文回答有關閏平年的問題,心想珊姍現在應該會答應他的求婚了吧。當文文開心地來找珊珊時,珊珊為了確保她後半輩子的幸福,給了他另一個問題:"I will marry you if you can tell me how many leap years there are between year a and year b, inclusive." 意思是「如果你可以告訴我西元 a 年和 b 年之間 (含) 有幾個閏年,我就嫁給你!」
輸入說明 : 
輸入只有一行,含有兩個由空白隔開的整數 a, b (1752<a≤b≤10000)。
輸出說明 : 
請輸出一個整數代表 a 年與 b 年之間有幾個閏年。
範例輸入 : help
2012 2016
範例輸出:
2
提示 : 
標籤:
出處: 


import java.util.Scanner;

public class d532 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int a=sc.nextInt();
int b=sc.nextInt();
int total=0;
for(int i=a;i<=b;i++){
if(i%4==0&&i%100!=0 || i%400==0)
total++;
}
System.out.println(total);
}

}

}

沒有留言:

張貼留言