POJ2262
来源:互联网 发布:无线传感器网络的概念 编辑:程序博客网 时间:2024/05/27 00:45
题目:http://acm.pku.edu.cn/JudgeOnline/problem?id=2262
思路:先建素数表。。然后二分查找。。。
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- public class Main {
- public static void main(String[] args) throws NumberFormatException,
- IOException {
- List<Integer> prime = new ArrayList<Integer>();
- boolean[] b = new boolean[1000000];
- int s = 0;
- int a1 = 0;
- int a2 = 0;
- int max = 0;
- int min = 0;
- int mid = 0;
- BufferedReader read = new BufferedReader(new InputStreamReader(
- System.in));
- prime.add(2);
- loop: for (int i = 3; i <= 1000000; i += 2) {
- if (!b[i]) {
- for (int j = 3; j <= Math.sqrt(i); j += 2) {
- if (i % j == 0) {
- continue loop;
- }
- }
- prime.add(i);
- for (int k = 2; i * k < 1000000; k++) {
- b[i * k] = true;
- }
- }
- }
- while ((s = Integer.parseInt(read.readLine())) != 0) {
- loop: for (int i = 0; i < prime.size(); i++) {
- a1 = prime.get(i);
- a2 = 0;
- min = i;
- max = prime.size() - 1;
- while (max >= min) {
- mid = (max + min) / 2;
- if (a1 + prime.get(mid) == s) {
- a2 = prime.get(mid);
- break loop;
- } else if (a1 + prime.get(mid) > s) {
- max = mid - 1;
- } else {
- min = mid + 1;
- }
- }
- }
- if (a2 != 0) {
- System.out.printf("%d = %d + %d/n", s, a1, a2);
- } else {
- System.out.println("Goldbach's conjecture is wrong.");
- }
- }
- }
- }
- POJ2262
- poj2262
- poj2262
- poj2262
- poj2262
- poj2262
- poj2262
- POJ2262
- poj2262
- poj2262
- POJ2262
- poj2262
- poj2262
- poj2262
- POJ2262
- Poj2262
- poj2262 Goldbach's Conjecture
- POJ2262 Goldbach's Conjecture
- C#与VB.Net之间的区别
- 当心隐式类型转换导致重载函数产生二义性
- C# 清空panel中所有控件
- Cookie和Session专题
- C#异步数据接收串口操作类
- POJ2262
- 巧妙从进程中判断出病毒和木马
- 虚拟内存状态
- 操作系统内存管理
- 虚拟内存使用
- 内存映射文件
- 十款ASP的内容管理系统(CMS)系统综合评比
- 获取M$ SQL Server用户表的字段信息
- 写时拷贝技术与共享节