一个用C++编写的哥德巴赫猜想,帮改改。。

来源:百度知道 编辑:UC知道 时间:2024/06/04 15:32:14
#include <iostream.h>
int panduan(int y){
int x=2;
while (y%x!=0)
x++;
return (y==x);
}
class stack{
private :
int p1,p2,q;
public:
void chaifen(int m){
q=m;
for(p1=1;p1<q/2+1;p1++){
p2=q-p1;

if (panduan(p1)&&panduan(p2))
cout<<q<<"="<<p1<<"+"<<p2<<";";
}
};

};
void main(){
stack a;
int n;
cout<<"请输入一个大于6的偶数:"<<endl;
cin>>n;
cout<<"现在输出这个数的所有哥德巴赫猜想的可能性:";
a.chaifen(n);
}
请大家帮帮忙,没有看明白这个。。。

你把P1先赋值为1把他带入panduan函数里,那个while循环不就是个死循环了
先mark,再慢慢看看

#include <iostream>
using namespace std;
bool panduan(int y){ //编程规范,用bool类型,不用int类型
int x = 2;
while (y % x != 0)
x++;
return (y == x);
}
class stack{
private :
int p1, p2, q;
public:
void chaifen(int m){
q = m;
for(p1 = 3; p1 < q/2 + 1; p1++){ //1并不是素数,所以从3开始
p2 = q - p1;

if (panduan(p1)&&panduan(p2))
cout << q << "=" << p1 << "+" << p2 << ";" << '\t';
}
}

};
int main(){
stack a;
long n; //实验数字如果非常大,是没有办法测试的,所以尽可能把长度设大
cout << "请输入一个大于6的偶数:" << endl;
cin >> n;
cout << "现在输出这个数的所有哥德巴赫猜想的可能性:" << endl;
a.chaifen(n);
cout << endl;
system("pause");
return 0;