staticScannerin=newScanner(System.in); staticintn= in.nextInt(); //读取基础数据 staticintr= in.nextInt(); //读取长度 staticint[] book = newint[n+1];//标记数据是否被使用 staticint[] out = newint[r+1]; //设置输出长度
publicstaticvoidmain(String[] args) {
dfs(1); }
publicstaticvoiddfs(int step) { inti=0; if (step > r) {
for (i=1;i<=r-1;i++){ //判断高位大于低位 if (out[i]<out[i+1]){ return; } } for (i = 1; i <=r; i++) { System.out.print(out[i]); } System.out.println(""); return; } for (i = n; i > 0; i--) { //深度优先算法 if (book[i] == 0) { out[step]=i; book[i]=1; dfs(step+1); book[i]=0; } } return; } }