POJ 1595 素数打表水题
【题意简述】:给出N和C,让我们求出N以内的包含N的素数,然后依据若N以内的素数为奇数个,就将中间2*c-1个素数输出;若为偶数个。就将中间2*c个素数输出。
【分析】:仅仅要题意理解就简单了。
详见代码:
// 224K 16Ms #include<iostream> using namespace std; #define N 2000 bool isprime[N]; int prime[N],nprime; void doprime(int n) { int i,j; nprime = 1; memset(isprime,true,sizeof(isprime)); isprime[1] = 0; prime[0] = 1; for(i = 2;i<=n;i++) { if(isprime[i]) { prime[nprime++] = i; for(j = i*i;j<=n;j+=i) { isprime[j] = false; } } } } int main() { int n,c; while(cin>>n>>c) { doprime(n); //acout<<nprime<<endl; if(nprime<2*c) { cout<<n<<" "<<c<<": "; for(int i = 0;i<nprime;i++) cout<<prime[i]<<" "; cout<<endl<<endl; } else { if(nprime%2==0)//擦!。!!! { cout<<n<<" "<<c<<": "; for(int i = (nprime-2*c)/2;i<(nprime + 2*c)/2;i++) cout<<prime[i]<<" "; cout<<endl<<endl; } else { cout<<n<<" "<<c<<": "; for(int i = (nprime-(2*c-1))/2;i< (nprime + (2*c-1))/2;i++) cout<<prime[i]<<" "; cout<<endl<<endl; } } } return 0; }