原文:https://qduoj.com/problem/9/点击打开链接
Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer n(n≤10000).
For each test case, print the number of ways.
3 9
0 2
本来以为需要线性筛 但是貌似暴力选素数也能过 while语句里用scanf会超时 换成cin就不会 不知道为什么 不知可有大佬能解释下
简单的打表+二重循环 另外注意在循环的时候要有从小到大选 不然会重复
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int a[10000];
int main()
{
for(int i=2;i<10000;i++)
{
int n=i;
if(a[i]==0)
while(n<10000)
{
n+=i;
if(n<10000)
{
a[n]=1;
}
}
}
a[0]=1;
a[1]=1;
int m=0;
int b[10000];
int head=0;
for(int i=0;i<10000;i++)
if(a[i]==0)
{
b[head]=i;
head++;
}
while(cin>>m)
{
int num=0;
for(int i=0;i<head;i++)
for(int j=i;j<head;j++)
if((m-b[i]-b[j])>=b[j])
{
if(a[m-b[i]-b[j]]==0)
num++;
}
printf("%d\n",num);
}
}