[ACM]Odd Sum

Odd Sum
Given a range [a, b], you are to find the summation of all the odd integers in this range. For example, the summation of all the odd integers in the range [3, 9] is 3 + 5 + 7 + 9 = 24.

Input
There can be at multiple test cases. The first line of input gives you the number of test cases, T ( 1<= T <= 100). Then T test cases follow. Each test case consists of 2 integers a and b ( 0 <= a <= b <= 100) in two separate lines. Output
For each test case you are to print one line of output - the serial number of the test case followed by the summation of the odd integers in the range [a, b].

Sample Input

2
1
5
3
5

Sample Output

Case 1: 9
Case 2: 8



/*
2009.4.6 Revised
Root :: Contest Volumes :: Volume CVII
Odd Sum
10783
UVa Online Judge
*/

#include <iostream>
#include <vector>

using namespace std;

int oddSum( int x, int y );

int main()
{
int caseNumberInput;

while( (cin >> caseNumberInput) != 0 )
{

vector<int> iv;

for ( vector<int>::size_type caseLoopCounter = 0;
caseLoopCounter != caseNumberInput;
caseLoopCounter )
{
int oddRangeOne, oddRangeTwo;
cin >> oddRangeOne;
cin >> oddRangeTwo;

int oddRangeTemp;
if ( oddRangeOne > oddRangeTwo )
{
oddRangeTemp = oddRangeTwo;
oddRangeTwo = oddRangeOne;
oddRangeOne = oddRangeTemp;
}

iv.push_back(oddRangeOne);
iv.push_back(oddRangeTwo);

}


vector<int> ivec;
for ( vector<int>::size_type caseLoopCounter = 0;
caseLoopCounter != iv.size();
caseLoopCounter )
{
//cout << iv[caseLoopCounter] << " ";
if( caseLoopCounter % 2 == 0 )
ivec.push_back
( oddSum(iv[caseLoopCounter],
iv[caseLoopCounter 1]) );

}

for ( vector::size_type counter = 0;
counter != ivec.size(); counter)
{
cout << "Case " << counter 1 << ": " <<
ivec[counter] << endl;

}


}

return 0;

}

int oddSum( int x, int y )
{
int sum = 0;
for(int loopCounter = x ; loopCounter <= y; loopCounter )
{
if( loopCounter % 2 != 0 )
sum = sum loopCounter;
}

return sum;
}


0 Response to "[ACM]Odd Sum"