蓝桥模拟赛 结果填空:素数个数

用 0,1,2,3 \cdots 70,1,2,37 这 88 个数组成的所有整数中,质数有多少个(每个数字必须用到且只能用一次)。

提示:以 00 开始的数字是非法数字。

暴力解决:

import java.util.Scanner;
import java.util.Arrays;

public class M {
	static int s;
	static int sum;
    //素数判断函数
	public static boolean su(double n){
		double s=Math.sqrt(n);
		for(int i=2;i<=s;i++)
			if(n%i==0)return false;
	return true;
}
	public static void main(String[] args) {
		//暴力排列组合
		for (int a = 1; a < 8; a++) {
			for (int b = 0; b < 8; b++) {
				for (int c = 0; c <8; c++) {
					for (int d = 0; d <8; d++) {
						for (int e = 0; e < 8; e++) {
							for (int f = 0; f < 8; f++) {
								for (int g = 0; g < 8; g++) {
									for (int h = 0; h < 8; h++) {
										
											s=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)&&su(s)){												
													//System.out.println(s);												
												sum++;
										}		
									}
								}
							}
						}
					}
				}
			}

		}

		System.out.println(sum);
	}
}

下面是参考别人的1-9排列组合
import java.util.Scanner;
import java.lang.Math;

public class Main {

	 public static void main(String[] args) {
	        String str[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
	        permutation(str, 0, str.length);
	    }

	    static void swap(String[] str, int start, int end) {
	        String tmep = str[start];
	        str[start] = str[end];
	        str[end] = tmep;
	    }

	    static void permutation(String[] str, int start, int end) {

	        if (start == end - 1) {
	            for (int i = 0; i < end; i++) {
	                System.out.print(str[i]);
	            }
	            System.out.println();
	        } else {

	            for (int i = start; i < end; i++) {
	                if (i == 0 && str[0].equals("0"))
	                    continue;
	                swap(str, start, i);
	                permutation(str, start + 1, end);

	                swap(str, start, i);
	            }
	        }
	    }
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Clark-dj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值