Problem D: C/C++经典程序训练7---求某个范围内的所有素数

版权声明: https://blog.csdn.net/t_jeey/article/details/79517595

Problem D: C/C++经典程序训练7---求某个范围内的所有素数

Time Limit: 1 Sec  Memory Limit: 64 MB

Description

求小于n的所有素数,按照每行10个显示出来。

Input

输入整数n(n<10000)。

Output

每行10个依次输出n以内的所有素数。

Sample Input

100

Sample Output

2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97

HINT

#include<stdio.h>
#include<math.h>
int main()
{
	int n,m;
	int i,j;
	int x=0;
	scanf("%d",&n);
	if(n<1000)
	{
		for(i=2;i<n;i++)
		{
			m=sqrt(i);
			for(j=2;j<=m;j++)
				if(i%j==0)
					break;
			if(j==m+1)
			{
				if(x!=0)	printf(" ");
				printf("%d",i);
			
				x++;
			}
			if(x==0)continue;
			if(x%10==0)
			{
				printf("\n");
				x=0;
			}
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/t_jeey/article/details/79517595