題目: UVa - 10783 - Odd Sum

題目說明

第一個數字代表有幾筆測資,每筆測資有兩個數字,題目要求算出兩數字間的奇數和 ( 包含兩數字 )。

解題思路

使用 for 迴圈直接做即可。

參考解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
int a, b, T, casenum = 1;

cin >> T;

while (T--)
{
int ans = 0;

cin >> a >> b;

for (int i = a; i <= b; ++i)
if (i % 2 == 1)
ans += i;

cout << "Case " << casenum << ": " << ans << endl;
++casenum;
}
}