神奇6位数
有一个6位的正整数,它有个很神奇的性质:
分别用2 3 4 5 6去乘它,得到的仍然是6位数,并且乘积中所包含的数字与这个6位数完全一样!只不过是它们的顺序重新排列了而已。
请计算出这个6位数。
这是一个整数,请通过浏览器提交答案,不要填写任何多余的内容(比如说明性的文字)
该题使用回溯法就能轻松解决了!!
package 第五届;
import java.util.Arrays;
public class Exe47 {
public static void main(String[] args) {
int num[] =
new int[]{
2,
3,
4,
5,
6};
for (
int i =
100000; i <
1000000; i++) {
dfs(i,num,
0);
}
}
private static void dfs(
int i,
int[] num,
int count) {
if (count>=num.length) {
System.
out.println(i);
return ;
}
char[] pre =
null;
char[] now =
null;
int temp = i*num[count];
pre = String.valueOf(i).toCharArray();
now = String.valueOf(temp).toCharArray();
Arrays.sort(pre);
Arrays.sort(now);
if (
new String(pre).equals(
new String(now))) {
dfs(i,num,count+
1);
}
}
}
答案:142857
打赏一点钱,帮我买杯咖啡,继续创作,谢谢大家!