ȫ����
img
c++����

#include<iostream>
using namespace std;
const int N = 10;
int path[N];
bool st[N] = { 0 };
int n;
void dfs(int u)
{
	if (u > n) {
		for (int i = 1; i <= n; i++) {
			printf("%d ", path[i]);
			
		}
		puts("");
	}
	else {
		for (int i = 1; i <= n; i++) {
			if (!st[i]) {
				st[i] = true;
				path[u] = i;
				dfs(u + 1);
				st[i] = false;
			}
		}
	}
}
int main() {
	cin >> n;
	dfs(1);
	return 0;
}

java����

import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

public class Main {
    private static int[] path;
    private static boolean[] st;
    private static int n;
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    private static void dfs(int u) throws Exception {
        if (u == n) {
            for (int i = 0; i < n; i ++ )
                bw.write(path[i] + 1 + " ");
            bw.write("\n");
        } else {
            for (int i = 0; i < n; i ++ )
                if (!st[i]) {
                    path[u] = i;
                    st[i] = true;
                    dfs(u + 1);
                    st[i] = false;  // �ָ��ֳ�
                }
        }
    }

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        path = new int[n];
        st = new boolean[n];

        dfs(0);

        bw.flush();
    }
}

posted on 2023-05-14 21:05  许七安gyg  阅读(12)  评论(0编辑  收藏  举报
$(document).ready(function() { // 禁止右键 $(document).bind("contextmenu", function(){return false;}); // 禁止选择 $(document).bind("selectstart", function(){return false;}); // 禁止Ctrl+C 和Ctrl+A $(document).keydown(function(event) { if ((event.ctrlKey&&event.which==67) || (event.ctrlKey&&event.which==86)) { //alert("对不起,版权所有,禁止复制"); return false; } }); });