# UVa 10783 ### 題目連結:[UVa10783](http://domen111.github.io/UVa-Easy-Viewer/?10783) ### 題述: 給你一個範圍 a 到 b ,請你找出 a 與 b 之間所有奇數的和。 例如:範圍 [ 3 , 9 ] 中所有奇數的和就是 3 + 5 + 7 + 9 = 24 。 --- 輸入的第一列有一個整數 T (1 ≦ T ≦ 100),代表以下有多少組測試資料。 每組測試資料為兩列,包含兩個數 a 與 b (0 ≦ a ≦ b ≦ 100)。 --- 每組測試資料輸出一列,內容為 a 及 b 間所有奇數的和。 ### c++ code: ```cpp= #include<bits/stdc++.h> using namespace std; int main () { //------------------------------------------------ #ifdef local freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //------------------------------------------------ int n ; int a ; int b ; int m = 1 ; cin >> n ; int k = n ; while ( n && (m <= k) ) { cin >> a >> b ; if ( a%2 == 0 ) { a += 1 ; } if ( b%2 == 0 ) { b -= 1 ; } int sum = 0 ; for ( int i = a ; i <= b ; i += 2 ) { sum += i ; } cout << "Case " << m << ": " << sum << endl ; n -= 1 ; m += 1 ; } } ``` :::success **``sample input``** 2 1 5 3 5 ::: :::success **``sample output``** Case 1: 9 Case 2: 8 ::: #### [返回首頁](https://hackmd.io/@fkleofk/APCS#10783) ###### tags: `APCS選修` `C++` `UVa`