題目連結: https://zerojudge.tw/ShowProblem?problemid=c022
# 解題思路
用 for 迴圈將區段內的奇數都加起來,可以先將偶數都變成奇數,免得算錯
# 程式碼
#include <iostream> | |
using namespace std; | |
int main(){ | |
int n,cnt=1; | |
cin>>n; | |
while(n--){ | |
long long int a,b,sum=0; | |
cin>>a>>b; | |
if(a%2==0) | |
a++; | |
if(b%2==0) | |
b--; | |
for(;a<=b;a+=2) | |
sum+=a; | |
cout<<"Case "<<cnt<<": "<<sum<<endl; | |
cnt++; | |
} | |
} |