On each segment from 10*n to 10*n+9 such numbers exactly 5. Therefore, it is enough to count the number of such segments, and process of boundary segments. Let sumdig(n) - Fonkoze, which gives the remainder from dividing the sum of the digits of n is 2. Then: int s0=(B/10-A/10-1)*5; int s1=(10+sumdig(A/10)-A%10)/2; int s2=(2+B%10-sumdig(B/10))/2; return s0+s1+s2;
But if trite (1+B-A)/2 (without a fractional part) ? Sort of converge for small numbers :-)
Take a number A, define for him the parity of the sum of numbers, then argue as follows: if A is odd, then A+1 is even, if A is even, then A+1 is odd. Exception is done for the transition xx9 - xy0 (I think there is xx9 and xy0 have odinakovy parity). In General, alternating run to B. I would have done so.
Do I understand the conditions of the problem?
Is given an arbitrary range, for example from 2000 to 5000. Need for each number, for example 2013 to add up the numbers, 2 + 0 + 1 + 3 and if the resulting number is even, increment by 1 ?
ie a head-on solution
1) Cycle: created array with the data in the specified range 2) Cycle: number of Disassembled components, folded, divided into two, determined even or odd 3) If even increased the counter to 1 4) Brought the result at the end of the program
The fastest is in this case, the mathematical solution with no cycles, but the school whether it is level?