# 更快的算法来计算一个范围内的特定整数可以分割多少个数字(Faster algorithm to count how many numbers are divisible by a specific integer in a range)

int a,b,c,d=0;
cin>>a>>b>>c;
for (int i=a;i<=b;i++)
{
if (i%c==0){d++;}
}
cout<<d;

int a,b,c,d=0;
cin>>a>>b>>c;
for (int i=a;i<=b;i++)
{
if (i%c==0){d++;}
}
cout<<d;

So this is the code, a..b is the number range, c is the divisor, and d counts the multiples of c. For example when a=5, b=15, c=3, d equals 4, because "6, 9, 12, 15" are the multiples between 5 and 15. I need to find faster way to do this, can anyone help?

## 最满意答案

int lower = (a + c - 1) / c; // find lowest divisor (round up)
int upper = b / c;           // find higher divisor (round down)
d = upper - lower + 1;       // get no of divisors

One way is to do it like this (no loops required):

int lower = (a + c - 1) / c; // find lowest divisor (round up)
int upper = b / c;           // find higher divisor (round down)
d = upper - lower + 1;       // get no of divisors

For your example case, lower will be 2, upper will be 5, giving d equal to 4.

2014-11-06

## Java将ObjectInputStream结果转换为ArrayList(Java cast ObjectInputStream result to ArrayList)

EOFException是“文件结束”异常。 这意味着当没有更多数据要读取时（已经在文件末尾），您尝试读取数据。 这是因为您正在读取2个对象，但是您只发送1.而不是第二次调用in.readObject() ，只需转换您已经读过的对象： list = (List<Edge>) inObject; EOFException is an "End Of File" exception. It means you attempted to read data when there is no more