题意:输入一个数判断是不是素数,并规定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. 使用sqldeveloper连接服务器端数据库

  2. objective c, property, copy

    @property (copy) nsmutablearray *array -copy, as implemented by mutable Cocoa classes, always return ...

  3. Object-c 构造、析构函数

    一.构造函数 在OC中凡是已init开头的函数我们都称之为构造函数,在声明构造函数的时候,不带参数的一般直接声明为“-(id)init”,带参数的一般声明为“-(id)initWith...”. @i ...

  4. To zero

    Let bygone be bygone. Now  everything changed. In fact, everything occurs to me cause I am a loser i ...

  5. TEXT 3 Food firms and fat-fighters

    TEXT 3 Food firms and fat-fighters 食品公司与减肥斗士 Feb 9th 2006 From The Economist Global Agenda Five lead ...

  6. cf-Round542-Div2-B(贪心)

    题目链接:http://codeforces.com/contest/1130/problem/B 思路: 贪心题.定义结构体数组a,a[i].x[0],a[i].x[1]分别表示i出现的第一个下标和 ...

  7. luoguP1004 方格取数(四维DP)

    题目链接:https://www.luogu.org/problemnew/show/P1004 思路: 这道题是四维DP的模板题,与luoguP1006传纸条基本相似,用f[i][j][k][l]表 ...

  8. keras—多层感知器识别手写数字算法程序

    #coding=utf-8 #1.数据预处理 import numpy as np #导入模块,numpy是扩展链接库 import pandas as pd import tensorflow im ...

  9. 没有无线路由,如何让手机使用电脑的网络xyytit

    前言: 智能手机已经越来越普遍,但国内的无线网络的步伐还是没有跟上智能机的脚步.纵使3G,4G已经相继推出,但国内的资费价格着实有点不接地气,所以无线wifi无疑是智能机使用最多的.各大软件上.设备商 ...

  10. python之面向对象之类变量和实例变量

    #Auther Bob #--*--conding:utf-8 --*-- #这里的变量全部都是实例变量 class Role(object): def __init__(self,name,role ...