2020年6月1日 星期一

C語言小教室-3

C語言小教室

前情提要:這裡會針對一些題目做解釋,紀錄我們一路寫程式的心路歷程.

題目一:(UVA10783) Odd Sum :

Time Limit: 3 sec

Description

Given a range [ab], 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 [ab]. 

Sample Input

Sample Output

Sample IO Generation

2

1

5

3

5

Case 1: 9

Case 2: 8

IO Generation(tested feature)





 
#include <stdio.h>
   宣告主函式 
int main()
{
宣告a和b用來儲存兩數. 
int a,b,n;
scanf("%d",&a);
for(int i=1;i<=a;i++)
{
int d=0;
scanf("%d%d",&b,&n);
for(int i=b;i<=n;i++)
{
if(i%2==1)d+=i;
}
printf("Case %d: %d\n",i,d);
}
return 0;
}
待編輯 

沒有留言:

張貼留言