题意:输入一个数判断是不是素数,并规定2不是素数。

析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了。。。

不说了,直接上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath> using namespace std;
typedef long long LL;
const int maxn = 16000 + 10;
int prime[maxn]; void init(){
memset(prime, 0, sizeof(prime));
for(int i = 2; i < sqrt(maxn+0.5); ++i) if(!prime[i])
for(int j = i * i; j < maxn; j += i)
if(!prime[j]) prime[j] = 1;
} int main(){
init();
int n, kase = 1;;
while(scanf("%d", &n), n > 0){
printf("%d: ", kase++);
if(1 == n || 2 == n || prime[n]) puts("no");
else puts("yes"); }
return 0;
}

HDU 2161 Primes (素数筛选法)的更多相关文章

  1. HDOJ(HDU) 2161 Primes(素数打表)

    Problem Description Write a program to read in a list of integers and determine whether or not each ...

  2. 数学--数论--HDU 2136(素数筛选法)

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  3. POJ 3978 Primes(素数筛选法)

    题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...

  4. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

  5. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

  6. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  7. HDU_2136——最大质因数,素数筛选法

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  8. 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。

    废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...

  9. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

随机推荐

  1. <U+FEFF> character showing up in files. How to remove them?

    You can easily remove them using vim, here are the steps: 1) In your terminal, open the file using v ...

  2. Haskell语言学习笔记(46)Parsec(3)

    Applicative Parsing 使用 Applicative 式的 Parser. 包括使用 (<$>), (<*>), (<$), (<*), (*> ...

  3. dapper.net 转载

    Dapper.NET——轻量ORM   Dapper.NET使用 本文目录 Dapper.NET使用 1.为什么选择Dapper 2.以Dapper(4.0)为例. 2.1 在数据库中建立几张表. 2 ...

  4. Javascript系列:总体理解

    js是一个脚本客户端(浏览器)语言.和sql html类似.多练习. 没有排错的经验,弱类型语言,浏览器解释执行,出错也不会报错 预备 <!DOCTYPE html PUBLIC "- ...

  5. 获得Variant类型

    function GetVariantType(const v: variant): string; begin   case TVarData(v).vType of     varEmpty: r ...

  6. 遇到的IE不兼容问题总结

    IE浏览器兼容问题困扰多时,由于IE6不在进行修补,IE6的考虑也越来越少,有些IE遇到的不兼容的现象想做一个总结 1宽度或高度:IE的是width+border+margin+padding goo ...

  7. plot绘图

    plot绘图 坐标系图(折线图) 折线图用于显示随时间或有序类别的变化趋势 plt.plot(x,y,format_string,**kwargs) y:Y轴数据,列表或数组,必选 x:X轴数据,列表 ...

  8. Windows自带的端口转发工具netsh使用方法_DOS/BAT

    Windows自带的端口转发工具netsh使用方法_DOS/BAT   作者:用户 来源:互联网 时间:2017-02-22 17:24:30 netsh 端口转发 摘要: 下面的代码在windows ...

  9. VScode 安装必备

    1.运行程序:

  10. oracle中的分支与循环语句

    分支语句 if的三种写法一, if 2 < 1 then dbms_output.put_line('条件成立'); end if; 二, if 2 < 1 then dbms_outpu ...