# 【題解】ZeroJudge c022: 10783 – Odd Sum

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c022
【解題想法】

• 題目保證 a≦b
• 迴圈解：找到離 a (含) 最近的奇數，開始兩個一數，加進sum。

#### 【方法-1】數值解

```#include<iostream>
using namespace std;

int main(){
int T;
cin >> T;
for (int i = 1; i <= T; i++) {
int a, b;
cin >> a >> b;
if (a % 2 == 0) {
a += 1;
}
if (b % 2 == 0) {
b -= 1;
}
int num=(b-a)/2+1;
int total;
total=(a+b)/2*num;
cout << "Case " << i << ": ";
cout << (a + b) / 2 * ((b - a) / 2 + 1) << "\n";
}
}
```

#### 【方法-2】雙重迴圈

```#include <iostream>
using namespace std;

int main() {
int T, a, b;
cin >> T;
for (int Case = 1; Case <= T; Case++) {
cin >> a >> b;
int sum = 0;
if (a % 2 == 0) {
a += 1;
}
for (int i = a; i <= b; i += 2) {
sum += i;
}
cout << "Case " << Case << ": " << sum << "\n";
}
return 0;
}
```

Python code (credit: Amy Chou)

```T = int(input())
for TC in range(1, T+1):
a = int(input())
b = int(input())

if a % 2 == 1:
start = a
else:
start = a+1

ans = 0
for i in range(start, b+1, 2):
ans += i
print(f"Case {TC}: {ans}")
```