HDOJ(HDU) 2161 Primes(素数打表)
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
给你一个数,判断它是不是素数,是素数输出**: yes,不是就输出
**: no
注意:1和2要输出no。
还有,判断结束的标志是n<=0.
import java.util.Arrays;
import java.util.Scanner;
public class Main{
static boolean db[] = new boolean[16001];
public static void main(String[] args) {
prime();
Scanner sc = new Scanner(System.in);
int t=0;
while(sc.hasNext()){
int n =sc.nextInt();
if(n<=0){
break;
}
if(db[n]){
System.out.println((++t)+": yes");
}else{
System.out.println((++t)+": no");
}
}
}
private static void prime() {
Arrays.fill(db, true);
db[1]=false;
db[2]=false;
for(int i=2;i<=Math.sqrt(db.length);i++){
for(int j=i+i;j<db.length;j+=i){
if(db[j]){
db[j]=false;
}
}
}
}
}
HDOJ(HDU) 2161 Primes(素数打表)的更多相关文章
- (step7.2.2)hdu 2161(Primes——判断是否是素数)
题目大意:输入一个n,判断您是否是素数.. 解题思路:简单数论 代码如下: /* * 2161_1.cpp * * Created on: 2013年8月31日 * Author: Administr ...
- HDU 2161 Primes (素数筛选法)
题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostrea ...
- HDU 2161 Primes
http://acm.hdu.edu.cn/showproblem.php?pid=2161 Problem Description Write a program to read in a list ...
- HDOJ(HDU) 2521 反素数(因子个数~)
Problem Description 反素数就是满足对于任意i(0< i < x),都有g(i) < g(x),(g(x)是x的因子个数),则x为一个反素数.现在给你一个整数区间[ ...
- hdu 2521 反素数(打表)
反素数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- UVA1213Sum of Different Primes(素数打表 + DP)
题目链接 题意:选择k个素数,使得和为N(1120)的方案数: 筛选出 <= N 的素数,然后就背包 写的时候没初始dp[0][0] = 1;而且方案数也没相加,真是弱逼 #include &l ...
- hdu 4135 Co-prime (素数打表+容斥原理)
题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数, ...
- hdu 5104 素数打表水题
http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...
- HDU 4548 美素数(打表)
HDU 4548 美素数(打表)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88159#problem/H 题目 ...
随机推荐
- datagrid后台给每列添加js方法
protected void dgExhList_ItemDataBound(object sender, DataGridItemEventArgs e) { string param = &quo ...
- Eclipse 打开时“发现了以元素'd:skin'”开头的无效内容。此处不应含有子元素(转)
打开 Eclipse 时,如图所示: 解决办法: 把有问题的 devices.xml 文件删除,再把 sdk 里面 tools\lib 下的这个文件拷贝到你删除的那个文件夹里,重启 eclipse 就 ...
- An App ID with Identifier 'xxxxxx’ is not available. Please ....
1.完全关闭Xcode; 2.找到钥匙串,将钥匙串(Keychain)中的对应证书移除: 3.再次打开Xcode,通过 Preferences - Account 4. 删除原先的账号重新登录, 搞定 ...
- iOS里面如何同时使用开启ARC的库 和 没有开启 ARC的库,ARC 与非 ARC同时存在的问题
旧工程配置arc方案: 1,直接在targets->build phases中修改compiler Flags,是否支持arc.添加:-fobjc-arc,就可以让旧项目支持arc. 新工程配置 ...
- POJ2584 T-Shirt Gumbo 二分图匹配(网络流)
#include <cstdio> #include <cstring> #include <algorithm> const int inf=0x3f3f3f3f ...
- 『重构--改善既有代码的设计』读书笔记----Extract Class
在面向对象中,对于类这个概念我们应该有一个清晰的责任认识,就是每个类应该只有一个变化点,每个类的变化应该只受到单一的因素,即每个类应该只有一个明确的责任.当然了,说时容易做时难,很多人可能都会和我一样 ...
- underscorejs-shuffle学习
2.21 shuffle 2.21.1 语法 _.shuffle(list) 2.21.2 说明 返回一个随机乱序的list副本数组, 使用 Fisher-Yates shuffle 来进行随机乱序. ...
- HTML5的Server-Sent Events (SSE)
HTML5有一个Server-Sent Events(SSE)功能,允许服务端推送数据到客户端.(通常叫数据推送).我们来看下,传统的WEB应用程序通信时的简单时序图: 现在Web App中,大都有A ...
- 【Linux】vi编辑器命令
1)进入vi的命令 vi filename : 打开或新建文件,并将光标置于第一行首 vi +n filename : 打开文件,并将光标置于第n行首 ...
- PHPCMS二层栏目调用
{pc:content action="category" catid="0" num="25" siteid="$siteid& ...