作了一个歌德巴赫猜想的程序分享下

来源:互联网 发布:域名怎样备案 编辑:程序博客网 时间:2024/05/21 15:44
 #include"stdafx.h"
#include"iostream"
using namespace std;
int IsJishu(int x){

if (x % 2 == 0)return 0;
return 1;


}

int IsSushu(int n){

for (int i = 2; i <= (int)sqrt(n); i++)
if (n%i == 0) return 0;
return 1;
}

int main(){

int x, i, j, k;
int a[1000] = { 0 };//1—x的存储所有素数
int m = 0;
int r = 0;
while (true)
{
while (true)
{
cout<<" 请输入要验证的数(歌德巴赫猜想)"<<endl;
cin >> x;
if (x < 5)
cout << "输入的数必须大于5,请重新输入" << endl;
else if (0 == IsJishu(x))
{
cout << "输入的数必须是奇数,请重新输入" << endl;

}

else
{
break;
}
}

for (i = 1; i < x; i++)
{
if (IsSushu(i)){ a[r++] = i; m++; }

}

for (i = 0; i <= m; i++)
for (j = 0; j <= m; j++)
for (k = 0; k <= m; k++)
if (x == a[i] + a[j] + a[k])cout << x << "=" << a[i] << "+" << a[j] << "+" << a[k] << endl;
}

return 0;
}
1 0