素数个数

用 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