close

Problem Description

文文愛偶數,無獨有「偶」地,珊珊也愛偶數。珊珊除了收藏偶數以外,每次她收到一些數字時,她還會把其中的偶數挑出來把玩並予以加總。今天珊珊又收到了一個範圍的連續整數,請問這次她從這段數字中所收集到的偶數的總和是多少?    

The Input

輸入只有一行,其中含有兩個由空白隔開的整數 a, b (0 ≤ a ≤ b ≤ 2147483647)。

The Output

請輸出一個整數,代表 a 與 b 之間 (含 a 與 b) 所有偶數的和,(答案會 ≤ 2147483647)。

Sample Input

2     5

Sample Output

6

-----* Problem from【ZeroJudge, an Online Judge System For Beginners

My Answer

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  #include<iostream>
using namespace std;
int main(void)
{
  int a, b;
  while(cin>>a>>b){
    int sum=0;
    for(int i=a; i<=b; ++i){
      if(i%2==0) sum+=i;
    }
    cout<<sum<<endl;
  }
  return 0;
}
 

 


arrow
arrow
    全站熱搜

    兔老大 發表在 痞客邦 留言(0) 人氣()