http://cpe.cse.nsysu.edu.tw/
2014/05/27 成果報告
http://cpe.cse.nsysu.edu.tw/doc/CPE_report_140527.pdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
ryCpe2014_001.py | |
http://uva.onlinejudge.org/external/107/10783.html | |
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 | |
''' | |
SampleInput= ''' | |
2 | |
1 | |
5 | |
3 | |
5 | |
''' | |
SampleOutput= ''' | |
Case 1: 9 | |
Case 2: 8 | |
''' | |
TestInput= ''' | |
3 | |
1 | |
10 | |
20 | |
30 | |
100 | |
200 | |
''' | |
TestOutput= ''' | |
Case 1: 25 | |
Case 2: 150 | |
Case 3: 7650 | |
''' | |
def 解題(inp, outp= None): | |
if outp != None: | |
S= outp.strip().split('\n') | |
I= inp.strip().split('\n') | |
T= int(I[0]) | |
O= '' | |
for i in range(T): | |
a= int(I[2*i+1]) | |
b= int(I[2*i+2]) | |
s= 0 | |
for x in range(a,b+1,2): | |
s+=x | |
o= 'Case %d: %d'%(i+1,s) | |
#print(o) | |
if outp != None: assert (o==S[i]), (o, S[i]) | |
O += o + '\n' | |
O= O.strip() | |
#print(O) | |
return O | |
if __name__=='__main__': | |
output0= 解題(SampleInput, SampleOutput) | |
print(output0) | |
output1= 解題(TestInput, TestOutput) | |
print(output1) | |
沒有留言:
張貼留言