3611 - 组素数
Time Limit : 1 秒
Memory Limit : 128 MB
素数就是不能再进行等分的数。比如:2 3 5 7 11 等。
9 = 3 * 3 说明它可以3等分,因而不是素数。
我们国家在1949年建国。如果只给你 1 9 4 9 这4个数字卡片,可以随意摆放它们的先后顺序(但卡片不能倒
着摆放啊,我们不是在脑筋急转弯!),那么,你能组成多少个4位的素数呢?<br />
比如:1949,4919 都符合要求。
请你提交:能组成的4位素数的个数,不要罗列这些素数!!
(这是蓝桥杯真题,原题为结果填空题。这里以编程题的形式供大家练习)
Input
Output
Examples
Input Format
Output Format
Solution C++
#include<iostream> #include<algorithm> using namespace std; int p[4]={1,4,9,9}; int isprime(int n) { int i; if(n<=1) return 0; for(i=2;i*i<=n;i++) { if(n%i==0) return 0; } return 1; } int main() { int cnt=0; do { if(isprime(p[0]*1000+p[1]*100+p[2]*10+p[3])) cnt++; } while(next_permutation(p,p+4)); cout<<cnt; return 0; }
Solution Java
public class Main{ public static void main(String []args){ int a[]={1499,1949,1994,4199,4919,4991,9149,9419,9914,9941,9194,9491}; int count=0; for(int i=0;i<a.length;i++){ if(f(a[i])){//System.out.println(a[i]); ++count;} } System.out.println(count); } public static boolean f(int n){ boolean b=true; for(int i=2;i<n;i++){ if(n%i==0) {b=false; break;} } return b; } }