题目:
Your task is to make function, which returns the sum of a sequence of integers.
The sequence is defined by 3 non-negative values: begin, end, step.
If begin value is greater than the end, function should returns 0
Examples
sequenceSum (2,2,2); // => 2 sequenceSum (2,6,2); // => 12 -> 2 + 4 + 6 sequenceSum (1,5,1); // => 15 -> 1 + 2 + 3 + 4 + 5 sequenceSum (1,5,3); // => 5 -> 1 + 4
我的:
int sequenceSum(unsigned int start, unsigned int end, unsigned int step)
{
int sum=0;
if (start > end)
return 0;
else {
for (start; start <= end; start+=step) {
sum += start;
}
}
return sum;
}
高手:
int sequenceSum(int start, int end, int step)
{
int sum = 0;
for(int tmp=start;tmp<=end; tmp+=step)
{
sum += tmp;
}
return sum;
}