码迷,mamicode.com
首页 > 其他好文 > 详细

codevs——1675 大质数 2

时间:2017-08-08 19:45:41      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:har   print   input   cstring   优化   小明   int   efault   content   

1675 大质数 2

 

 时间限制: 1 s
 空间限制: 1000 KB
 题目等级 : 钻石 Diamond
 
 
 
题目描述 Description

小明因为没做作业而被数学老师罚站,之后数学老师要他回家把第n个质数找出来。

小明于是交给聪明的你。请你帮忙!【wikioi-1530】

…………………………以上为背景…………………………

老师怀疑小明仅仅是找到第n个质数,于是又叫小明把1到n以内(不包括n)的质数全部找出来。小明又找到了你……

输入描述 Input Description

一个正整数n。

(1<=n<=1000000)

输出描述 Output Description

n以内的质数,每个一行。

样例输入 Sample Input

233

样例输出 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
101
103
107
109
113
127
131
137
139
149
151
157
163
167
173
179
181
191
193
197
199
211
223
227
229          //(不含n=233)

数据范围及提示 Data Size & Hint

注意优化算法

 

淼!!

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int n,ans,sum,tot;
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-) f=-1; ch=getchar();}
    while(ch>=0&&ch<=9) {x=x*10+ch-0; ch=getchar();}
    return x*f;
}
bool pd(int x)
{
    if(x==1) return false;
    for(int j=2;j*j<=x;j++)
     if(x%j==0) return false;
    return true;
}
int main()
{
    n=read();
    if(n>1) printf("2\n");
    for(int i=1;i<n;i+=2)
    {
        if(pd(i)) printf("%d\n",i);
      }  
    return 0;
}

 

codevs——1675 大质数 2

标签:har   print   input   cstring   优化   小明   int   efault   content   

原文地址:http://www.cnblogs.com/z360/p/7308213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!