POJ 刷题系列:2262. Goldbach's Conjecture

来源:互联网 发布:java 迭代器模式 编辑:程序博客网 时间:2024/05/29 19:15

POJ 刷题系列:2262. Goldbach’s Conjecture

传送门:POJ 2262. Goldbach’s Conjecture

题意:

给定一个大于4的数num,求两个奇素数使得num = p1 + p2.

思路:
打一个素数表,枚举小于num的素数p1,接着二分查找num-p2是否在素数表中,有则输出答案。

代码如下:

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.util.Arrays;import java.util.Map;import java.util.StringTokenizer;public class Main{    String INPUT = "./data/judge/201712/P2262.txt";    public static void main(String[] args) throws IOException {        new Main().run();    }    static final int MAX_N = 1000000 + 16;    boolean[] isPrime = new boolean[MAX_N];    int[] primes = new int[MAX_N];    int tot;    void seive() {        Arrays.fill(isPrime, true);        for (int i = 2; i < MAX_N; ++i) {            if (isPrime[i]) {                primes[tot++] = i;                for (int j = 2 * i; j < MAX_N; j += i) {                    isPrime[j] = false;                }            }        }    }    void solve(int num) {        for (int i = 0; i < tot && primes[i] < num; ++i) {            int p1 = primes[i];            int idx = Arrays.binarySearch(primes, 0, tot, num - p1);            if (idx >= 0) {                out.println(num + " = " + p1 + " + " + (num - p1));                return;            }        }        out.println("Goldbach's conjecture is wrong.");    }    void read() {        seive();        while (true) {            int num = ni();            if (num == 0) break;            solve(num);        }    }    FastScanner in;    PrintWriter out;    void run() throws IOException {        boolean oj;        try {            oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm");        } catch (Exception e) {            oj = System.getProperty("ONLINE_JUDGE") != null;        }        InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));        in = new FastScanner(is);        out = new PrintWriter(System.out);        long s = System.currentTimeMillis();        read();        out.flush();        if (!oj){            System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");        }    }    public boolean more(){        return in.hasNext();    }    public int ni(){        return in.nextInt();    }    public long nl(){        return in.nextLong();    }    public double nd(){        return in.nextDouble();    }    public String ns(){        return in.nextString();    }    public char nc(){        return in.nextChar();    }    class FastScanner {        BufferedReader br;        StringTokenizer st;        boolean hasNext;        public FastScanner(InputStream is) throws IOException {            br = new BufferedReader(new InputStreamReader(is));            hasNext = true;        }        public String nextToken() {            while (st == null || !st.hasMoreTokens()) {                try {                    st = new StringTokenizer(br.readLine());                } catch (Exception e) {                    hasNext = false;                    return "##";                }            }            return st.nextToken();        }        String next = null;        public boolean hasNext(){            next = nextToken();            return hasNext;        }        public int nextInt() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Integer.parseInt(more);        }        public long nextLong() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Long.parseLong(more);        }        public double nextDouble() {            if (next == null){                hasNext();            }            String more = next;            next = null;            return Double.parseDouble(more);        }        public String nextString(){            if (next == null){                hasNext();            }            String more = next;            next = null;            return more;        }        public char nextChar(){            if (next == null){                hasNext();            }            String more = next;            next = null;            return more.charAt(0);        }    }    static class D{        public static void pp(int[][] board, int row, int col) {            StringBuilder sb = new StringBuilder();            for (int i = 0; i < row; ++i) {                for (int j = 0; j < col; ++j) {                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));                }            }            System.out.println(sb.toString());        }        public static void pp(char[][] board, int row, int col) {            StringBuilder sb = new StringBuilder();            for (int i = 0; i < row; ++i) {                for (int j = 0; j < col; ++j) {                    sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));                }            }            System.out.println(sb.toString());        }    }    static class ArrayUtils {        public static void fill(int[][] f, int value) {            for (int i = 0; i < f.length; ++i) {                Arrays.fill(f[i], value);            }        }        public static void fill(int[][][] f, int value) {            for (int i = 0; i < f.length; ++i) {                fill(f[i], value);            }        }        public static void fill(int[][][][] f, int value) {            for (int i = 0; i < f.length; ++i) {                fill(f[i], value);            }        }    }    static class Num{        public static <K> void inc(Map<K, Integer> mem, K k) {            if (!mem.containsKey(k)) mem.put(k, 0);            mem.put(k, mem.get(k) + 1);        }    }}

这里写图片描述

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 有摩托车驾照想考小车驾照怎么办 分管副局长能直接安排工作吗怎么办 我在北京打工老婆没地方住怎么办 我的车扣了32分怎么办 济南万科地产投诉电话不管用怎么办 买手机被商家欺骗买到合约机怎么办 向消协投诉有用吗?我该怎么办? 我住南开区想办公租房不知怎么办 租房提前退房房东不退押金怎么办 体检时候眼睛有一只是弱视怎么办? b本被扣分9分了怎么办 工作调动后在新单位退休医保怎么办 社保卡和医保卡丢了怎么办 医保卡挂失后又找到了怎么办 医保卡丢失忘了卡号怎么办? 医保卡丢了怎么办又记不住卡号 住院发票丢了医保不给报销怎么办 住院期间被医院丢失了医保卡怎么办 大学时的医保卡毕业后丢了怎么办 用身份证注册的移动卡丢了怎么办 用别人身份证办的卡丢了怎么办 济南医保卡挂失后又找到了怎么办 单位没有给办理医保卡的老人怎么办 单位办的医保卡丢了怎么办 北京退休人员医保卡丢了怎么办 普通发票联丢了医保给报销怎么办 手机买好高铁票身份证丢了怎么办 买了高铁票身份证丢了怎么办 小米手机手电简打开不亮了怎么办 华为p7手机显示屏不亮了怎么办 红米手机3x屏幕不灵怎么办? 乐视1s手机字库坏了怎么办 三星c7手机左右两按钮不亮怎么办 手机摔了一屏碎了下黑屏了怎么办 三星手机摔了一下黑屏了怎么办 行驶证一年扣分超过50分怎么办 朋友去广西传销现在骗我怎么办 行驶证忘带交警查住了怎么办 行驶证正本丢了副本在怎么办 在杭州驾照12分扣完了怎么办 驾照审验期过了40天了怎么办