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

HduOJ 2162 - Primes

时间:2018-05-11 20:44:35      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:isp   namespace   color   链接   play   lap   view   target   math   

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2161

 

题意:判断n是不是素数,输入到0停止。题目规定1 2 都不是素数。

 

题解:筛素数。老题目。不过这次是普通筛23333.。之前做的题了。

 

技术分享图片
 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstdio>
 4 using namespace std;
 5 #define N 16001
 6 bool prime[N];
 7 void init(){
 8     for(int i = 0; i <= N ; i++){
 9         prime[i] = true;
10     }
11     
12     
13     for(int i = 3; i < N; i++){
14         int tot = (i + 1) / 2;
15         for(int j = 2; j <= tot; j++){
16             if(i % j == 0){
17                 prime[i] = false;
18                 break;
19             }
20         }
21     }
22     prime[1] = false;
23     prime[2] = false;
24      
25 }
26 int main(){
27     init();
28     int n;
29     int cas = 1;
30     while(scanf("%d",&n) , n>0){
31         printf("%d: ",cas++);
32         if(prime[n])
33             printf("yes\n");
34         else
35             printf("no\n");
36     }
37     return 0;
38 } 
View Code

 

HduOJ 2162 - Primes

标签:isp   namespace   color   链接   play   lap   view   target   math   

原文地址:https://www.cnblogs.com/Asumi/p/9025342.html

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