poj 1595 Prime Cuts

简介:

好久没有刷题了,第一题还算顺利,这道题难度不大,但是却有严格的输出格式,少打一个空行就直接presentation error了,不过改一下就AC了

用的素数筛,开始预处理素数,之后就只用读表了


AC的代码:

#include <stdio.h>

#define MAXN 1002

int prime[MAXN];  //内容0代表素数

//对数字进行预处理,直接打印出1002以内素数
void PickPrime()
{
	//先打出素数表
	prime[1]=0;    //开始标记prime[1]为素数
	int i,j;
	for(i=2;i<MAXN;i++)
	{
		if(prime[i]==0)		//如果prime[i]是素数就把他的倍数都筛掉
		{
			for(j=2*i;j<MAXN;j+=i)
				prime[j]=1;
		}
	}
}

int main()
{
	int n,c,reallPrime[MAXN];
	int i,j,start,end;

	PickPrime();

	while(scanf("%d",&n)!=EOF)
	{
		scanf("%d",&c);

		j=0;
		for(i=1;i<=n;i++)
			if(prime[i]==0)
				reallPrime[++j]=i;

		printf("%d %d:",n,c);

		//就是在j个数中选取中间c个数
		if(c<=j)
		{
			//有even个素数
			if(j%2==0)
			{
				start=(j-c*2)/2;
				end=start+c*2;
				for(i=start+1;i<=end;i++)
					printf(" %d",reallPrime[i]);
			}

			//有odd个素数
			else
			{
				start=(j-c*2+1)/2;
				end=start+c*2-1;
				for(i=start+1;i<=end;i++)
					printf(" %d",reallPrime[i]);
			}
		}

		else
		{
			for(i=1;i<=j;i++)
					printf(" %d",reallPrime[i]);
		}

		printf("\n\n");
	}

	return 0;
}


相关文章
UVa11679 - Sub-prime
UVa11679 - Sub-prime
54 0
HDU-1061,Rightmost Digit(快速幂)
HDU-1061,Rightmost Digit(快速幂)
|
机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
POJ 1775 (ZOJ 2358) Sum of Factorials
145 0
POJ 1844 Sum
POJ 1844 Sum
104 0
|
安全
D-POJ-3126 Prime Path
Description The ministers of the cabinet were quite upset by the message from the Chief of...
1126 0
|
人工智能 机器学习/深度学习
POJ 1775 (ZOJ 2358) Sum of Factorials
Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions t...
1146 0
POJ 1844 Sum
Description Consider the natural numbers from 1 to N. By associating to each number a sign (+ or -) and calculating the value of this expression we obtain a sum S.
817 0
容斥 - HDU 4135 Co-prime
Co-prime  Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean:  给你一个区间[l,r]和一个数n,求[l,r]中有多少个数与n互素。
906 0