22全排列:组素数
【题目】
组素数
素数就是不能再进行等分的数。比如:2 3 5 7 11 等。
9 = 3 * 3 说明它可以3等分,因而不是素数。
我们国家在1949年建国。如果只给你 1 9 4 9 这4个数字卡片,
可以随意摆放它们的先后顺序(但卡片不能倒着摆放啊,我们不是在脑筋急转弯!),那么,你能组成多少个4位的素数呢?
比如:1949,4919 都符合要求。
请你提交:能组成的4位素数的个数,不要罗列这些素数!!
注意:不要提交解答过程,或其它的辅助说明文字。
【分析】
这个题目有两种思想去做,
一种是递归的思想找出所有可能的组合,全排列,
1 import java.math.BigInteger; 2 import java.util.*; 3 4 public class Lanq { 5 6 public static void main(String[] args) { 7 组素数(); 8 } 9 10 public static void 组素数() { 11 Scanner sc = new Scanner(System.in); 12 int cnt=0; 13 int a[] = {9941, 9914, 9491, 9419, 9194, 9149, 4991, 4919, 4199, 1994, 1949, 1499}; 14 for(int i=0;i<12;i++) 15 { 16 if(isPrime(a[i]))cnt++; 17 } 18 System.out.println(cnt); 19 } 20 public static boolean isPrime(int n) 21 { 22 if(n==2||n==3)return true; 23 for(int i=3;i<n;i++) 24 { 25 if(n%i==0)return false; 26 } 27 return true; 28 }