|
Yes, the upstairs is right, I just saw this problem, the OddSum function should be written at least like this:
int OddSum (int m, int n)
{
// This is actually a summation problem of arithmetic sequence
int Begin, End, Sum = 0;
Begin = (m% 2 == 0? M + 1: m); // If m is an odd number, the starting item is m, otherwise m + 1
End = (n% 2 == 0? N-1: n); // If n is odd, the ending item is n, otherwise it is n-1
return (Begin + End) * ((End-Begin) / 2 + 1) / 2; // Sum formula of arithmetic sequence
} |
|