【题目链接】:https://nanti.jisuanke.com/t/25085
【题目描述】:
用 0,1,2,3⋯7 这 8 个数组成的所有整数中,质数有多少个(每个数字必须用到且只能用一次)。
提示:以 0 开始的数字是非法数字。
结果:
2668
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int cnt=0;
for(int a=1;a<=7;a++){
for(int b=0;b<=7;b++){
for(int c=0;c<=7;c++){
for(int d=0;d<=7;d++){
for(int e=0;e<=7;e++){
for(int f=0;f<=7;f++){
for(int g=0;g<=7;g++){
for(int h=0;h<=7;h++){
if(su(a*10000000+b*1000000+c*100000+
d*10000+e*1000+f*100+g*10+h)){
if(a!=b && a!=c && a!=d && a!=e && a!=f && a!=g && a!=h &&
b!=c && b!=d && b!=e && b!=f && b!=g && b!=h &&
c!=d && c!=e && c!=f && c!=g && c!=h &&
d!=e && d!=f && d!=g && d!=h &&
e!=f && e!=g && e!=h &&
f!=g && f!=h &&
g!=h)
cnt++;
}
}
}
}
}
}
}
}
}
System.out.println(cnt);
}
public static boolean su(long n){
for(int i=2;i*i<n;i++){
if(n%i==0){
return false;
}
}
return true;
}
}