• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

POJ-2909 Goldbach's Conjecture

题意:给定一个偶数n,求将其分解成两个素数的种数。

思路:水水。。。

题目链接:http://poj.org/problem?id=2909

 

View Code
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 const int N=40000;
10 
11 bool isprime[N];
12 int prime[N];
13 int n;
14 
15 int main(){
16     
17 //    freopen("data.in","r",stdin);
18 //    freopen("data.out","w",stdout);
19     
20     int cnt=0;
21     isprime[1]=true;
22     for(int i=2;i<N;i++){
23         if(!isprime[i]){
24             prime[++cnt]=i;
25             for(int j=2;i*j<N;j++)
26                 isprime[i*j]=true;
27         }
28     }
29     while(scanf("%d",&n),n){
30         int sum=0;
31         for(int i=1;prime[i]<=n/2;i++)
32             if(!isprime[n-prime[i]]) sum++;
33         printf("%d\n",sum);
34     }
35     return 0;
36 }
posted @ 2012-08-02 19:10  Hug_Sea  阅读(98)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3