例题7-4 素数环 Java
题目 输入正整数n,把整数1,2,3,…,n组成一个环,使得相邻两个整数之和为素数。输出时从整数1开始逆时针排列。同一个环应恰好输出一次。n<<16. 样例输入: 6 样例输出: 1 4 3 2 5 6 1 6 5 2 3 4 static int n ; static int [ ] A ; static int [ ] vis ; public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; n = sc . nextInt ( ) ; A = new int [ n ] ; A [ 0 ] = 1 ; vis = new int [ n + 1 ] ; dfs ( 1 ) ; } static void dfs ( int cur ) { if ( cur == n && isp ( A [ 0 ] , A [ n - 1 ] ) ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( A [ i ] ) ; } System . out . println ( ) ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( vis [ i ] == 0 &&