2016年1月15日 星期五

d491: 我也愛偶數 (swap 版)

內容 :
文文愛偶數,無獨有「偶」地,珊珊也愛偶數。珊珊除了收藏偶數以外,每次她收到一些數字時,她還會把其中的偶數挑出來把玩並予以加總。今天珊珊又收到了一個範圍的連續整數,請問這次她從這段數字中所收集到的偶數的總和是多少?
輸入說明 : 
輸入只有一行,其中含有兩個由空白隔開的整數 a, b (0 ≤ a, b ≤ 2147483647)。(a 不一定會小於等於 b 哦!)
輸出說明 : 
請輸出一個整數,代表 a 與 b 之間 (含 a 與 b) 所有偶數的和,(答案會 ≤ 2147483647)。
範例輸入 : help
5 2
範例輸出:
6
提示 : 
標籤:
出處: 


import java.util.Scanner;

public class d491 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int a=sc.nextInt();
int b=sc.nextInt();
int t;
if(a>b){
t=a;
a=b;
b=t;
}
long total=0;
for(int i=a;i<=b;i++){
if(i%2==0)
total+=i;
}
System.out.println(total);
}
}

}

沒有留言:

張貼留言