http://acm.hdu.edu.cn/showproblem.php?pid=2161

Problem Description
Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes. 
 
Input
Each input line contains a single integer. The list of integers is terminated with a number<= 0. You may assume that the input contains at most 250 numbers and each number is less than or equal to 16000.
 
Output
The output should consists of one line for every number, where each line first lists the problem number, followed by a colon and space, followed by "yes" or "no". 
 
Sample Input
1
2
3
4
5
17
0
 
Sample Output
1: no
2: no
3: yes
4: no
5: yes
6: yes
 
时间复杂度:$O(\sqrt{N})$
代码:

#include <bits/stdc++.h>
using namespace std; int N; int prime(int x) {
for(int i = 2; i * i <= x; i ++) {
if(x % i == 0)
return 0;
}
return 1;
} int main() {
int Case = 1;
while(~scanf("%d", &N)) {
if( N <= 0) break;
printf("%d: ", Case ++);
if(N == 1 || N == 2)
printf("no\n"); else {
if(prime(N))
printf("yes\n");
else
printf("no\n");
}
}
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. (step7.2.2)hdu 2161(Primes——判断是否是素数)

    题目大意:输入一个n,判断您是否是素数.. 解题思路:简单数论 代码如下: /* * 2161_1.cpp * * Created on: 2013年8月31日 * Author: Administr ...

  3. HDU 2161 Primes (素数筛选法)

    题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostrea ...

  4. hdu 5104 Primes Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5104 Primes Problem Description Given a number n, ple ...

  5. hdu 5104 Primes Problem(prime 将三重循环化两重)

    //宁用大量的二维不用量小的三维 #include <iostream> #include<cstdio> #include<cstring> using name ...

  6. [kuangbin带你飞]专题十四 数论基础

            ID Origin Title   111 / 423 Problem A LightOJ 1370 Bi-shoe and Phi-shoe   21 / 74 Problem B ...

  7. KUANGBIN带你飞

    KUANGBIN带你飞 全专题整理 https://www.cnblogs.com/slzk/articles/7402292.html 专题一 简单搜索 POJ 1321 棋盘问题    //201 ...

  8. UVA10200-Prime Time/HDU2161-Primes,例题讲解,牛逼的费马小定理和欧拉函数判素数。

                                                    10200 - Prime Time 此题极坑(本菜太弱),鉴定完毕,9遍过. 题意:很简单的求一个区间 ...

  9. [kuangbin带你飞]专题1-23题目清单总结

    [kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1321 棋盘问题POJ 2251 Dungeon MasterPOJ 3278 Catch That CowPOJ 3279 Fli ...

随机推荐

  1. 爬虫-爬虫介绍及Scrapy简介

    在编写案例之前首先理解几个问题,1:什么是爬虫2:为什么说python是门友好的爬虫语言?3:选用哪种框架编写爬虫程序 一:什么是爬虫? 爬虫 webSpider 也称之为网络蜘蛛,是使用一段编写好的 ...

  2. vimrc 配置

    " All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by" the call to : ...

  3. 001-快速排序(C++实现)

    快速排序的基本实现 快速排序算法是一种基于交换的高效的排序算法,它采用了分治法的思想: 1.从数列中取出一个数作为基准数(枢轴,pivot). 2.将数组进行划分(partition),将比基准数大的 ...

  4. QT Creator 转VS2017+QT工程

    因为刚学QT 不熟悉其QT Creator 的开发环境,所以打算用VS2017来开发,因为要跨平台所以打算写出来的代码可以放在linux上编译. 这时就需要将VS2017的工程转换成QT Creato ...

  5. Webservice HTTP

    由于项目需要:自己写了一个WebserviceDemo,把遇到的问题记下来. 方式一 :使用代理类来访问Webservice,此方式不讲解,感觉复杂(神坑). (生成的代理路径 C:\Users\ad ...

  6. Qt 5 最新信号和槽连接方式以及Lambda表达式

    最近学习Qt,发现新大陆,这里做下记录. 主要内容就是原始Qt4的信号槽连接方式,以及Qt5新版的连接方式,还有件事简单演示一下lambda表达式的使用方式 代码如下 /* * 作者:张建伟 * 时间 ...

  7. sql注入--高权限,load_file读写文件

    select '<?php eval($_POST[123]) ?>' into outfile '/var/www/html/1.php'; 1.MYSQL新特性限制文件写入及替代方法 ...

  8. Katalon 学习笔记(一)

      工具介绍: Katalon Studio是一个能提供一整套功能来实现Web,API和Mobile的全自动测试解决方案的自动化测试平台.Katalon Studio构建于开源Selenium和App ...

  9. jenkins--Jenkins+Git+coding+maven 实现自动化测试持续集成

    1.打开Jenkins官网,下载jenkins.war https://jenkins.io/download/ 2.将该war包直接放置到Tomcat的webapp下. 3.查看自己Tomcat的端 ...

  10. cookie的介绍和自动化中cookie的操作

    1 cookie是什么? cookie: 1. Cookie是一小段的文本信息:格式:python中的字典(键值对组成) 2. Cookie产生:客户端请求服务器,如果服务器需要记录该用户状态,就向客 ...