吠悟 @ 2020-02-09 11:59:19
#include<iostream>
#include<cmath>
using namespace std;
bool isprime(int x)
{
int s = sqrt(double(x));
for (int i = 2;i <= s;i++)
{
if (x % i == 0)
return false;
}
return true;
}
int main()
{
int n,count=0;
cin >> n;
int a[10000];
int min=99999, minx=0,miny=0,minz=0;
for (int i = 2; i <= n;i++)
{
if (isprime(i))
{
a[count] = i;
count++;
}
}
for (int i = 0;i <= count;i++)
{
for (int j = i;j <= count;j++)
{
for (int k = j;k <= count;k++)
{
if (a[i] + a[j] + a[k] == n)
{
if (a[i] < min)
{
min = a[i];
minx = a[i];
miny = a[j];
minz = a[k];
}
}
}
}
}
cout << minx << " " << miny << " " << minz;
}
by zhanghzqwq @ 2020-02-09 12:01:09
求质数没判断0和1
by ztxtjz @ 2020-02-09 12:03:03
只需要两次循环,每一次循环后就判一下质数,再判一下n减去这两个数是不是质数
by zhanghzqwq @ 2020-02-09 12:03:49
@吠悟 0和1不是质数
by 吠悟 @ 2020-02-09 12:06:13
@zhanghanzhang 不需要啊。。。我程序里是把小于等于n的质数全都储存到数组里面,这样就不用考虑0和1了吧
by Dimly_dust @ 2020-02-09 12:06:41
@吠悟
#include <bits/stdc++.h>
int num,Prime[100001],n;
bool notPrime[100001];
void init()
{
notPrime[1]=1;
for(int i=2; i<=n; ++i)
{
if(!notPrime[i])
Prime[++num]=i;
for(int j=1; j<=num&&i*Prime[j]<=n; ++j)
{
notPrime[i*Prime[j]]=1;
if(i%Prime[j]==0) break;
}
}
}
int main()
{
cin>>n;
init();
for(int i=1; i<=num; ++i)
{
for(int j=i; j<=num; ++j)
{
if(!notPrime[n-Prime[i]-Prime[j]]&&n-Prime[i]-Prime[j]>0)
{
cout<<Prime[i]<<Prime[j]<<n-Prime[i]-Prime[j];
return 0;
}
}
}
return 0;
}
你再试试
by 吠悟 @ 2020-02-09 12:07:57
@缥缈于尘 洛谷说编译失败。。。
by zhanghzqwq @ 2020-02-09 12:09:06
#include<iostream>
using namespace std;
bool isPrime(int n){
for(int i=2;i*i<=n;i++){
if(n%i==0){
return false;
}
}
return true;
}
int main(){
int n;
cin>>n;
for(int i=2;i<n;i++){
for(int j=2;j<n;j++){
if(isPrime(i)&&isPrime(j)&&isPrime(n-i-j)&&n-i-j>=2){
cout<<i<<" "<<j<<" "<<n-i-j<<endl;
return 0;
}
}
}
return 0;
}
开两层循环
by Dimly_dust @ 2020-02-09 12:10:04
@吠悟
在
#include <bits/stdc++.h>
后换行加
using namespace std;
by Dimly_dust @ 2020-02-09 12:10:24
抱歉
by 吠悟 @ 2020-02-09 12:11:28
@zhanghanzhang 谢谢大佬