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 ( 1T100). Then T test cases follow. Each test case consists of 2 integers a and b ( 0ab100) 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
题意:给定两个整数,求这两个数之间所有奇数的和。
思路:判断出两个正整数的奇偶性,然后累加,注意求和的参数的初始化!
AC源代码(C语言):
1 #include<stdio.h> 2 int main() 3 { 4 int a,b,T,i,j,sum; 5 while(scanf("%d",&T)==1) 6 { 7 for(i=1;i<=T;i++) 8 { 9 sum=0; 10 scanf("%d%d",&a,&b); 11 for(j=a;j<=b;j++) 12 { 13 if(j%2==1) sum+=j; 14 } 15 printf("Case %d:%d\n",i,sum); 16 } 17 break; //保证只输入T组数据!!!!! 18 } 19 return 0; 20 }
2013-03-14