当前位置: 代码迷 >> 综合 >> HDOJ 1397 Goldbach's Conjecture
  详细解决方案

HDOJ 1397 Goldbach's Conjecture

热度:112   发布时间:2023-10-21 19:23:19.0

HDACM 1397

因为输入的值小于2^15,所以可以采用打表的方式来做。简单暴力

import java.util.Scanner;public class Main{public static void main(String[] args) {boolean noPrime[] = new boolean[32768];for (int i = 2; i < noPrime.length; i++) {if (!noPrime[i]) {for (int j = i*2; j < noPrime.length; j+=i) {noPrime[j]=true;}}}Scanner sc = new Scanner(System.in);while (sc.hasNext()) {int n = sc.nextInt();if (n==0) {break;}int count = 0;for (int i = 2; i <= n/2; i++) {if (!noPrime[i]&&!noPrime[n-i]) {count++;}}System.out.println(count);}sc.close();}
}