题目描述:
abcdefghi分别表示1~9的不同的数字,ab可以被2整除,abc被3整除。。。。。。abcdefghi可以被9整除
代码:
import java.util.ArrayList;
public class yami {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 111111111; i <= 999999999; i++) {
int j, a = 10000000;
for (j = 2; j <= 9; j++) {
int b = i / a; // 依次获得N个高位组成的数字,例如123.第一次是1,第二次是12,第三次是123
if (b % j != 0)
break;
a /= 10;
}
// 经过了初步筛选,此时的数字i包含重复的数字,需要继续筛选
if (j == 10 && check(i))
System.out.println(i);
}
}
//该函数主要是检查有没有重复的,若果重复则返回false
private static boolean check(int x) {
// TODO Auto-generated method stub
int j = 1;
String str = String.valueOf(x); // 将 x 的字符串形式保存在 str 中
ArrayList<Integer> list = new ArrayList<Integer>(); // 该 list 用来检查 x 中的每一位是不是有相同的,如果不同就加入,否则直接返回
for (int i = 9; i >= 1; i--) {
int k = x / j; // 每次割掉数字的最后一个
int yushu = str.charAt(i - 1) - '0'; // 获取被割掉的数字
if (yushu == 0) { // 因为是1~9之内的数字,所以舍弃0
return false;
}
if (!list.contains(yushu)) { // 如果不包含该数字则加入 list
list.add(yushu);
} else {
return false;
}
}
return true;
}
}
测试结果: