读书人

算法三

发布时间: 2012-08-24 10:00:21 作者: rapoo

算法3

A non-negative integer is called heavy if the average value of its digits in decimal representation exceeds 7. Assume that 0 has an average value of its digits equal to 0.

For example, the number 8,698 is heavy because the average value of its digits is (8+6+9+8)/4 = 7.75. The number 53,141 has an average value of its digits of (5+3+1+4+1)/5 = 2.8, so it is not heavy.

Write a function:

class Solution { public int heavy_decimal_count(int A,int B); }

that, given two non-negative integers A and B, returns the number of heavy integers within the interval [A..B] (both ends included).

Assume that:

For example, given A=8,675 and B=8,689 the function should return 5, because there are five heavy integers within the range [8,675..8,689]:

?

8675    avg=6.508676    avg=6.758677    avg=7.008678    avg=7.25    HEAVY8679    avg=7.50    HEAVY8680    avg=5.508681    avg=5.758682    avg=6.008683    avg=6.258684    avg=6.508685    avg=6.758686    avg=7.008687    avg=7.25    HEAVY8688    avg=7.50    HEAVY8689    avg=7.75    HEAVY

?

Complexity:

读书人网 >编程

热点推荐