24小时热门版块排行榜    

查看: 882  |  回复: 3
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

yatou99

铜虫 (正式写手)

[求助] 对于编程的问题一无所知,小女子求助谁能帮忙解一道题,灰常感谢!

我们开设了C++的课程,可是我没有基础,连C语言都没学过,第一次上课,真是雾里看花,什么都不懂,课后老师还布置了一道貌似很简单的题,但是我真是不会做,希望哪位智者人士救救我吧!小女子将感激不尽啊!!!!!
题目是:从8到100000,找出每个偶数都可以分解成两个素数之和。请写出具体的代码。
(灰常感激您的帮助,谢谢您,您是一个好人!祝福您天天开心!)
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

yatou99

铜虫 (正式写手)

引用回帖:
2楼: Originally posted by huycwork at 2011-09-21 11:01:19:
这个题真的很简单,不过鉴于楼主不会C++,就来个C++代码则个:
[code]
#include <iostream>
#include <set>
#include <utility>

using namespace std;
class PrimeSet {
public:
        se ...

哈哈,谢谢咯!真感激你呀!
4楼2011-09-21 12:19:31
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 4 个回答

huycwork

金虫 (著名写手)

【答案】应助回帖


yatou99(金币+5): 谢谢! 2011-09-21 12:20:23
jjdg(金币+1): 感谢参与 2011-09-22 00:34:08
这个题真的很简单,不过鉴于楼主不会C++,就来个C++代码则个:
CODE:
#include
#include
#include

using namespace std;
class PrimeSet {
public:
        set primes;
        PrimeSet(int l, int r);
};

PrimeSet::PrimeSet(int l, int r){
        primes.insert(3);
        for(int i = 5; i < r; i += 2){
                set::iterator it;
                for(it = primes.begin(); it != primes.end(); ++it){
                        if(i%*it == 0)
                                break;
                }
                if(it == primes.end())
                        primes.insert(i);
        }
}

class PairMaker {
        const set
public:
        PairMaker(const set &ps);
        pair operator ()(int d);
};

PairMaker::PairMaker(const set &ps):primes(ps){}

pair PairMaker::operator()(int d){
        for(set::iterator it = primes.begin(); it != primes.end(); ++it){
                if(primes.count(d-*it)){
                        return make_pair(*it, d-*it);
                }
        }
        return make_pair(0, 0);
}

void print_pair(const pair &pair){
        cout< }

int main(){
        int left = 8, right = 100000;
        PrimeSet ps(left, right);
        PairMaker maker(ps.primes);
        for(int i = left; i <= right; i+=2){
                print_pair(maker(i));
        }
        return 0;
}

漩涡的中心有一块空地,空空的。
2楼2011-09-21 11:01:19
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

huycwork

金虫 (著名写手)

★ ★
jjdg(金币+2): 感谢 2011-09-22 00:34:18
换一个,上面那个属于错误示范
CODE:
#include
#include
#include

using namespace std;
class PrimeSet {
        vector primes;
public:
        PrimeSet(int l, int r);
        pair sumOfHomework(int d) const;
};

PrimeSet::PrimeSet(int l, int r):primes(r, true){
        for(int i = 2; i < r; ++i){
                if(!primes[i])
                        continue;
                for(int j = i+i; j < r; j += i){
                        primes[j] = false;
                }
        }
}

pair PrimeSet::sumOfHomework(int d) const{
        for(int i = 3; i < primes.size(); ++i){
                if(primes[i] && primes[d-i]){
                        return make_pair(i, d-i);
                }
        }
        return make_pair(0, 0);
}

class PairMaker {
        const PrimeSet ℙ
public:
        PairMaker(const PrimeSet &ps);
        pair operator ()(int d);
};

PairMaker::PairMaker(const PrimeSet &ps):primes(ps){}

pair PairMaker::operator()(int d){
        return primes.sumOfHomework(d);
}

void print_pair(const pair &pair){
        cout< }

int main(){
        int left = 8, right = 100000;
        PrimeSet ps(left, right);
        PairMaker maker(ps);
        for(int i = left; i <= right; i+=2){
                print_pair(maker(i));
        }
        return 0;
}

漩涡的中心有一块空地,空空的。
3楼2011-09-21 11:48:38
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 面上项目filecode邪修 +4 西山十月 2026-08-09 6/300 2026-08-10 00:16 by liufeng0619
[基金申请] 应该是93bebmhtak前后十一个字符比较关键 +7 Lanmanbaby 2026-08-09 14/700 2026-08-09 23:33 by Tide man
[基金申请] 2026国自然放榜时间 +4 布布和一二 2026-08-08 4/200 2026-08-09 22:24 by otj2008
[基金申请] 关于代码变化问题,想知道的进来 +15 且听虎啸 2026-08-07 22/1100 2026-08-09 22:13 by 布布和一二
[基金申请] 这样的filecode谁见过 +10 布布和一二 2026-08-08 21/1050 2026-08-09 17:21 by 天神眷顾
[基金申请] filecode与中标关系的预测 +5 布布和一二 2026-08-07 5/250 2026-08-09 16:15 by 袁向阳007
[基金申请] 关于filecode,很负责任的告诉大家 +6 爱看书的可乐 2026-08-08 7/350 2026-08-08 22:13 by a_niu
[考博] 【2027博士申请】纳米药物递送方向 20+3 13586093586 2026-08-03 5/250 2026-08-08 21:54 by 13586093586
[基金申请] 关于豆爷回答的JTJC与%2F数量 +5 yang182083 2026-08-06 7/350 2026-08-08 18:29 by zhanghaozhu
[基金申请] 国基金的申报应该改成非等额制,评价高的钱多评价低的钱少,但是增加资助率 +7 a089 2026-08-07 7/350 2026-08-08 18:05 by gltch
[基金申请] 关于filecode +4 布布和一二 2026-08-07 7/350 2026-08-07 22:55 by zhanghaozhu
[基金申请] 娱乐 +6 Tide man 2026-08-03 6/300 2026-08-07 22:40 by 铁帽子农民
[基金申请] 听说今天filecode变了 +24 布布和一二 2026-08-06 47/2350 2026-08-07 16:02 by zhiyanjiang
[基金申请] filecode变化情况 +6 布布和一二 2026-08-07 22/1100 2026-08-07 14:45 by 且听虎啸
[基金申请] 大家散了吧,后缀研究没有意义,别浪费时间了,过好目前的每一天,不要焦虑 +5 Tide man 2026-08-06 7/350 2026-08-07 13:11 by 医学老男孩
[基金申请] filecode +14 等待解的谜 2026-08-06 19/950 2026-08-07 12:20 by wlwhappy
[基金申请] 影响面上的因素 +8 布布和一二 2026-08-05 11/550 2026-08-06 10:41 by 宝贝虫子
[基金申请] 好消息?这个有何含义??? +8 Tide man 2026-08-05 10/500 2026-08-05 16:14 by xmuxiaoyu
[基金申请] 有没有H口的?有收到消息的吗? +3 超级海虾 2026-08-04 3/150 2026-08-04 17:26 by 学教育滴
[基金申请] 纯娱乐,不喜欢勿喷 +7 Tide man 2026-08-04 10/500 2026-08-04 15:10 by loufangrui
信息提示
请填处理意见