Problem A: 求素数
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 116 Solved: 3
[ Submit][ Status][ Web Board]
Description
设计一个程序,输出所有小于等于n(n为一个大于2的正整数)的素数。
要求:(1)每行输出10个素数。
(2)尽量采用较优的算法。
Input
50
Output
2 3 5 7 11 13 17 19 23
29 31 37 41 43 47
Sample Input
50
Sample Output
2 3 5 7 11 13 17 19 23
29 31 37 41 43 47
HINT
#include <iostream>
using namespace std;
#include <cstdio>
int panduan(int n)
{
if(n<2)return 0;
for(int i=2; i*i<=n; i++)
if(n%i==0)return 0;
return 1;
}
int main()
{
int n,s,i;
while(cin>>n)
{
s=0;
for(i=2; i<=n; i++)
{
if(panduan(i))
{
s++;
printf(" %2d",i);
if(s%10==0)cout<<endl;
}
}
cout<<endl;
}
return 0;
}