[URI Online Judge] – 1099 – Sum of Consecutive Odd Numbers II

Read an integer N that is the number of test cases. Each test case is a line containing two integer numbers X and Y. Print the sum of all odd values between them, not including X and Y.

Input

The first line of input is an integer N that is the number of test cases that follow. Each test case is a line containing two integer X and Y.

Output

Print the sum of all odd numbers between X and Y.

Sample Input Sample Output
7

4 5

13 10

6 4

3 3

3 5

3 4

3 8

0

11

5

0

0

0

12

Solution:

#include <cstdio>
int main(){
 int n, i, f, x, j, s;
scanf("%d", &n);
while(n--){
 s = 0;
scanf("%d %d", &i, &f);
if (i > f){ x = i; i = f; f = x; }
i++;
if (i % 2 == 0) i++;
for (j = i; j < f; j += 2) s += j;
printf("%d\n", s);
}
 return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s