张晓宇的博客空间

导航

一个小题目

这个是求由1-9组成的9位数,各位数字不能重复。

同时这个数字满足,前2位可以被2整除,前3位可以被3整除,前n位可以被n整除。

package com.java.test;

public class Number {
public static void main(String[] args) {

int thisNum = 0;
for(thisNum = 123456780; thisNum < 987654322 ; thisNum++)   //查找123456789 - 987654322 所有数
if(test(thisNum)){                    //test 判断各个位上数字是否满足为1-9且不重复。
if(testPer(thisNum)){     //testPre判断前n位能否被n整除。
System.out.println("this num:" + thisNum);
}
}
}

/**

*/test 判断各个位上数字是否满足为1-9且不重复。

*/
public static boolean test(int num){
boolean flag = true;
for(int i = 0;i < 9;i++){
for(int j = 0;j < i;j++){
int n = num % (int)Math.pow(10, i+1) / (int)Math.pow(10, i);
int m = num % (int)Math.pow(10, j+1) / (int)Math.pow(10, j);
if(n==m || n==0 || m==0 ){
flag = false;
}else{
flag = flag && true;
}    
}
}
return flag;
}

/**

*testPre判断前n位能否被n整除。

*/

public static boolean testPer(int num){
boolean flag = true;
for(int i = 2;i <= 9;i++){
int perNum = num / (int)Math.pow(10, 9-i);
if(perNum % i == 0){
flag = flag && true;
}else{
flag = false;
}
}
return flag;
}
}

 

posted on 2014-03-11 09:30  zhuiyv_blog  阅读(136)  评论(0编辑  收藏  举报