莲花楼楼主


私信TA

用户名:dotcpp0719623

访问量:589

签 名:

总有人间一两风,填我十万八千梦

等  级
排  名 2117
经  验 2364
参赛次数 0
文章发表 31
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

有人在通往牛逼的路上一路狂奔

解题思路:

注意事项:

参考代码:

import java.util.Scanner;

public class Main {
	static int[] arr=new int[4];
	static int count=0;//一行输出6个
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n=scanner.nextInt();
        while(n-->0) {
        	int[] usedArray=new int[4];
        	arr[0]=scanner.nextInt();
        	arr[1]=scanner.nextInt();
        	arr[2]=scanner.nextInt();
        	arr[3]=scanner.nextInt();
        	dfs(0,usedArray,0);
        	System.out.println();
        }
    }
	public static void dfs(int num,int[] usedArray,int step) {
		if(step==4) {//递归终止条件:生成一个完整的排列
			System.out.print(num+" ");
			count++;
			if(count==6) {//6个一行
				System.out.println();//换行
				count=0;//重置计数器
			}
		}
		for(int i=0;i<4;i++) {
			if(usedArray[i]==0) {//该数字未被使用过,可以用
				num=num*10+arr[i];//比如12,变成了120+3=123
				usedArray[i]=1;//标记用过的数字
				dfs(num,usedArray,step+1);//递归
				num/=10;//回溯
				usedArray[i]=0;//取消标记
			}
		}
	}
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区