From 1 to 9, the nine digits form a nine-digit number with no duplicates. If a number is extracted from the left, it must be divisible by 1. The number composed of two numbers can be divisible by 2. The number composed of three numbers can be divisible by 3, and so on... The number of each fetch is obtained from the left. Q: What is this number? Returns the numberAlgorithm.
CodeAs follows:
Import Java. util. arrays; </P> <p> public class magicdiv {<br/> private int [] used = new int [10]; <br/> Public void find () {<br/> arrays. fill (used, 0); <br/> used [5] = 1; <br/> DFS (0, 1 ); <br/>}< br/> Public void DFS (INT pre, int POS) {<br/> If (Pos> 9) {<br/> system. out. println (pre); <br/> return; <br/>}< br/> int TMP = Pre * 10; <br/> If (Pos = 5) {<br/> DFS (TMP + 5, POS + 1); <br/>}else {</P> <p> for (INT I = 1; I <10; I ++) {<br/> If (used [I] = 0) {<br/> int cur = TMP + I; <br/> If (cur % Pos = 0) {<br/> used [I] = 1; <br/> DFS (cur, POS + 1 ); <br/> used [I] = 0; <br/>}</P> <p >}< br/> Public static void main (string [] ARGs) {<br/> New magicdiv (). find (); <br/>}</P> <p>}
From: http://yangguo.javaeye.com/blog/801820