zoukankan      html  css  js  c++  java
  • HDU2098 分拆素数和

    Problem Description
    把一个偶数拆成两个不同素数的和,有几种拆法呢?
     
    Input
    输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束。
     
    Output
    对应每个偶数,输出其拆成不同素数的个数,每个结果占一行。
     
    Sample Input
    30
    26
    0
     
    Sample Output
    3
    2
     
    解题思路:先欧拉筛数然后在进行试探分析是否可以两个素数的和为这个偶数。
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            
            int count =0;
            int num = 100000;
            boolean[] bl = new boolean[100000000];
            int[]  primeList = new int[num];
            for(int i =2;i < num;i++){
                if(!bl[i]){
                    primeList[count++] = i;
                }
                for(int j =0;j < count;j++){
                    if(i*primeList[j] > num){
                        break;
                    }
                    bl[i*primeList[j]] = true;
                    if(i%primeList[j]==0){
                        break;
                    }
                }
            }
            
            for(;;){
                int sum = input.nextInt();
                if(sum ==0){
                    break;
                }
                int count1 =0;
                for(int i =2;i < sum/2;i++){
                    if(!bl[i]&&!bl[sum-i]){
                        if(i!=sum-i)
                        count1++;
                        System.out.println(i+" " +(sum-i));
                    }
                    
                }
                System.out.println(count1);
            }
            
        }
    }
  • 相关阅读:
    Spark SQL ---一般有用
    idea快捷键
    04.Scala编程实战 ---没看
    03.Scala高级特性 ---没看
    02.Actor编程 ---没看
    01.Scala编程基础 ---没看
    附6、Storm面试题目答疑 ---一般有用
    扩展运算符
    ES6新增数组方法(部分)
    for of 循环
  • 原文地址:https://www.cnblogs.com/ke-T3022/p/8385311.html
Copyright © 2011-2022 走看看