hdu1016JAVA

时间:2021-09-10 07:24:04

import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int kase=0,n;
public static int [] circles=new int [50];
public static boolean[] flag=new boolean[50],primes=new boolean[50];
public static void intiPrimes()
{Arrays.fill(primes,true);
for(int i=2;i<=30;i++)
{
for(int j=i+i;j<50;j+=i)
{
if(j%i==0)
primes[j]=false;
}
}
primes[0]=primes[1]=false;

}
public static void DFS(int i)
{if(i==n)
{if(primes[1+circles[n-1]]){
for(int j=0;j<n;j++)
{if(j==n-1)
System.out.print(circles[j]);
else
System.out.print(circles[j]+" ");

}
System.out.println();
}
return;
}
else
{for(int j=2;j<=n;j++)
{if(flag[j]){
if(primes[j+circles[i-1]]&&(j+circles[i-1]&1)!=0)
{
flag[j]=false;
circles[i]=j;
DFS(i+1);
flag[j]=true;
}
}
}
}
return;
}
public static void main(String[] args)
{Scanner in=new Scanner(System.in);
intiPrimes();
while(in.hasNext())
{n=in.nextInt();
System.out.println("Case"+" "+(++kase)+":");
if(n==1)
{System.out.println(1);
System.out.println();
continue;
}
else if((n&1)!=0)
{System.out.println();
continue;
}
Arrays.fill(flag,true);
circles[0]=1;
flag[1]=false;
DFS(1);
System.out.println();
}
}
}

System.out.println("Case"+" "+(++kase)+":");注意这个地方的输出方式;

相关文章