原题:
OutputThe output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by "yes" or "no".
Sample Input
1 2 3 4 5 17 0Sample Output
1: no 2: no 3: yes 4: no 5: yes 6: yes解:
#include<iostream>
#include<cstring>
#include<cmath>
#define N 16000
using namespace std;
bool isprime[16005];//0为质数,1为合数
int pri[16005];
void prime()
{
int cnt=1;
memset(pri,0,sizeof(pri));
memset(isprime,0,sizeof(isprime));
isprime[1]=1;
for(int i=2;i<=sqrt(N);i++)//从2开始筛选,倍数都为合数
{
if(!isprime[i])//如果是质数(0取反==1 进入if 存储数值为0)
{
pri[cnt++]=i;//储存质数(本题无用)
for(int j=i*2;j<=N;j+=i)//从2*i开始递推 每次增加i
{
isprime[j]=1;//
}
}
}
isprime[2]=1;//若开始就对2赋值则到时无法进入if
}
int main()
{
prime();
int t=250;
for(int i=1;i<=t;i++)
{
int k;
cin>>k;
if(k>0)
{
if(isprime[k]==0) cout<<i<<": "<<"yes"<<endl;
else cout<<i<<": "<<"no"<<endl;
}
else break;
}
}