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;
}
时间: 2024-10-05 15:02:52

POJ 1595 素数打表水题的相关文章

POJ 2739 Sum of Consecutive Prime Numbers(素数打表水题)

[题意简述]:题意很简单,就是用连续的素数加和,计算有多少个这样的连续的素数数列可以使这个序列的和等于输入的数. [分析]:很经典的素数模板,基本所有有关素数的题,我都会使用这个模板. // 268K 16Ms #include<iostream> using namespace std; #define N 10000 bool isprime[N]; long long prime[1300],nprime; // 注意long long void doprime() { long lon

POJ 3030. Nasty Hacks 模拟水题

Nasty Hacks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13136   Accepted: 9077 Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of malicious software which teenagers may use to fool their friends.

POJ1595_Prime Cuts【素数】【水题】

Prime Cuts Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10464 Accepted: 3994 Description A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In this problem you are to write a program that w

poj 3444 Wavelet Compression 模拟水题

水题,直接贴代码. //poj 3444 //sep9 #include <iostream> using namespace std; const int maxN=260; int a[maxN],b[maxN]; int main() { int i,n,m; while(scanf("%d",&n)==1&&n){ for(i=1;i<=n;++i) scanf("%d",&a[i]); m=1; while

【POJ】Cow Multiplication(水题)

Cow Multiplication http://poj.org/problem?id=3673 题意:输入两个数A B,比如123和45   然后算123*45这个运算是指1*4 + 1*5 + 2*4 + 2*5 + 3*4 + 3*5 = 54. 思路:水题. #include<iostream> #include<cmath> #include<cstring> using namespace std; typedef long long ll; const

POJ3518_Prime Gap【素数】【水题】

Prime Gap Time Limit: 5000MSMemory Limit: 65536K Total Submissions: 8499Accepted: 4983 Description The sequence of n ? 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime number

poj 1004:Financial Management(水题,求平均数)

Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126087   Accepted: 55836 Description Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has deci

POJ 3641 素数打表+快速幂 简单题

给出2个数,p和a,2<p<=1e9,1<a<p 若p满足下面2个条件,输出yes,否则输出no 1.p不是素数 2.有a^p=a(mod p) 先判断第一个条件: 本来想用一个数组is_prime[i]表示i是不是素数的,明显,这里p太大,数组开不下 若p不是素数的话, 则p必有p=b*c,其中b<=c, 则(sqrt(p))^2=b*c,则b<=sqrt(p)<=10^4.5<10^5 所以若在10^5内存在数b满足b<p&&p%b

POJ 2656 Unhappy Jinjin(水题)

[题意简述]:找到两数之和最大的那一天. [分析]:这个....代码就贴在题目下啊. #include <stdio.h> int main(){ while(1) { int i, n; int maxday, maxvalue = -1; scanf("%d", &n); if (n == 0) break; for (i = 1; i <= n; i++) { int a, b; scanf("%d%d", &a, &