素数个数

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

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

public class Main {
    static int[] a;
    static int count;
    public static void main(String[] args) {
        int n = 76543210;
        a = new int[n + 1];
        for(int i = 2; i < a.length; i ++) a[i] = 1;
        
        for(int i = 2; i * i <= n; i ++) {
            if(a[i] == 1) {
                for(int j = i * i; j <= n; j += i) {
                    a[j] = 0;
                }
            }
        }
        
        for(int i = 2; i < a.length; i ++) {
            if(a[i] == 1) {
                if(f(String.valueOf(i))) {
                    count ++;
                    System.out.println(i);
                }
            }
        }
        System.out.println();
        System.out.println(count);
    }
    private static boolean f(String s) {
        int count = 0;
        int a = s.indexOf('0');
        int aa = s.indexOf('0') - s.lastIndexOf('0');
        
        int b = s.indexOf('1');
        int bb = s.indexOf('1') - s.lastIndexOf('1');
        
        int c = s.indexOf('2');
        int cc = s.indexOf('2') - s.lastIndexOf('2');
        
        int d = s.indexOf('3');
        int dd = s.indexOf('3') - s.lastIndexOf('3');
        
        int e = s.indexOf('4');
        int ee = s.indexOf('4') - s.lastIndexOf('4');
        
        int f = s.indexOf('5');
        int ff = s.indexOf('5') - s.lastIndexOf('5');
        
        int g = s.indexOf('6');
        int gg = s.indexOf('6') - s.lastIndexOf('6');
        
        int h = s.indexOf('7');
        int hh = s.indexOf('7') - s.lastIndexOf('7');
        
        int i = s.indexOf('8');
        int j = s.indexOf('9');
        
        if(a != -1) count ++;
        if(aa == 0) count ++;
        if(b != -1) count ++;
        if(bb == 0) count ++;
        if(c != -1) count ++;
        if(cc == 0) count ++;
        if(d != -1) count ++;
        if(dd == 0) count ++;
        if(e != -1) count ++;
        if(ee == 0) count ++;
        if(f != -1) count ++;
        if(ff == 0) count ++;
        if(g != -1) count ++;
        if(gg == 0) count ++;
        if(h != -1) count ++;
        if(hh == 0) count ++;
        if(i == -1) count ++;
        if(j == -1) count ++;
        
        if(count == 18) return true;
        else return false;
    }
}

 

转载于:https://www.cnblogs.com/jizhidexiaobai/p/8650514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值