如果一个数能够被组成它的各个非0数字整除,则称它是完美数。例如:1-9都是完美数,10,11,12都是完美数,但是13就不是完美数(因为13不能被组成它的数字“1”和“3”整除)。 现在给定正整数x,y,求x和y之间(包含x和y)一共有多少个完美数。输入数据1<=x<=y<=2000000000。 例如: x = 1, y = 13,输出12; x = 13,y = 13,输出0; x = 100,y = 1000,输出106。
自己写了个答案,不过运行超过3秒。挑战失败,求大神帮忙啊!
/**
*
* @author Joshua
* @version 2014-1-15
*
*/
public class PerfectInteger {
public static int howmany(int a, int b) {
int c = 0, y = 0, x = a;
while (a < b + 1) {
y = x % 10;// 末位数
x /= 10;// 每次除去最低位
if (y > 1 && a > (a / y) * y)
x = ++a;
if (x == 0) { // 处理到了最后一位
x = ++a;
c++;
}
}
return c;
}
public static void main(String args[]) {
long s = System.currentTimeMillis();
System.out.println(howmany(1, 200000000));
System.out.println(System.currentTimeMillis() - s);
}
}