標題:
20101002-d491: 我也愛偶數 (swap 版)
[打印本頁]
作者:
buy
時間:
2010-10-2 10:58
標題:
20101002-d491: 我也愛偶數 (swap 版)
把答案貼上來吧~
作者:
chuangjoy
時間:
2010-10-2 11:01
#include<iostream>
using namespace std;
int main(void){
long a, b, n, m;
while(cin >> a >> b){
if(a < b){
if(a % 2 == 0 && b % 2 == 0){
n = ((b - a)/ 2 + 1);
m = (a + b) * n / 2;
cout << m << endl;
}else if(a % 2 == 0 && b % 2 == 1){
n = (((b - 1)- a)/ 2 + 1);
m = (a + (b - 1)) * n / 2;
cout << m << endl;
}else if(a % 2 == 1 && b % 2 == 0){
n = ((b - a + 1)/ 2);
m = ((a + 1) + b) * n / 2;
cout << m << endl;
}else{
n = ((b - a)/ 2);
m = ((a + 1) + (b - 1)) * n / 2;
cout << m << endl;
}
}else{
if(a % 2 == 0 && b % 2 == 0){
n = ((a - b)/ 2 + 1);
m = (b + a) * n / 2;
cout << m << endl;
}else if(a % 2 == 0 && b % 2 == 1){
n = (((a - 1)- b)/ 2 + 1);
m = (b + (a - 1)) * n / 2;
cout << m << endl;
}else if(a % 2 == 1 && b % 2 == 0){
n = ((a - b + 1)/ 2);
m = ((b + 1) + a) * n / 2;
cout << m << endl;
}else{
n = ((a - b)/ 2);
m = ((b + 1) + (a - 1)) * n / 2;
cout << m << endl;
}
}
}
return 0;
}
複製代碼
麻煩~
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2