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 题目 ...
 
随机推荐
- Simple screenshot that explains the non-static invocation.
			
Here is the code: /* Instance invocation in the memory: */ package kju.obj; import static kju.print. ...
 - ASP 调用dll(VB)及封装dll实例
			
ASP调用dll及封装dll实例,封装为dll可以提供运行效率,加密代码. 打开VB6,新建ActiveX DLL 2.在工程引用中加入Microsoft Active Server Pages Ob ...
 - 寒哥教你学 iOS - 经验漫谈(转)
			
转自http://www.cocoachina.com/ios/20150907/13339.html 本篇文章主要讲解 4个问题 load妙用 aop面向切面编程 NSNumber Or Int @ ...
 - svn的初级使用
			
首先呢 你需要下载一个软件 比如说是 Cornerstone. 进行安装好之后 然后 然后输入账号密码 就可以了 然后去xcode去进行相关的配置 点击第二个进入 偏好设置 点击最下边的+ 点击第二 ...
 - WebSocket基于javaweb+tomcat的简易demo程序
			
由于项目需要,前端向后台发起请求后,后台需要分成多个步骤进行相关操作,而且不能确定各步骤完成所需要的时间 倘若使用ajax重复访问后台以获取实时数据,显然不合适,无论是对客户端,还是服务端的资源很是浪 ...
 - (java)从零开始之--装饰者设计模式
			
装饰者设计模式:简单定义:增强一个类的功能,而且还可以让这些装饰类互相装饰. 应用场景:当要在某个功能的基础上扩充功能,并且扩充的功能具有大量排列组合,通过继承关系会衍生出大量子类,这时候用装饰者模式 ...
 - 对数据预处理的一点理解[ZZ]
			
数据预处理没有统一的标准,只能说是根据不同类型的分析数据和业务需求,在对数据特性做了充分的理解之后,再选择相关的数据预处理技术,一般会用到多种预处理技术,而且对每种处理之后的效果做些分析对比,这里面经 ...
 - windows编程中 一些前缀区分 IDR和IDD
			
IDC_:控件的ID命名前缀(Control) IDM_:菜单的ID命名前缀(Menu) IDD_:对话框的ID命名前缀(Dialog) IDR_:资源的ID命名前缀(Resource) IDS_:字 ...
 - 在2015中使用V12版本的ReportView控件,会导致winform窗体不能正常打开
			
在2015中使用V12版本的ReportView控件,会导致winform窗体不能正常打开,使用V10版本没问题,但2015中默认使用的就是V12版本,所以需要避免使用V12版本
 - 关于call 与 apply 那些事
			
1.定义: call : 调用一个对象的一个方法,以另一个对象替换当前的对象. apply : 应用某一对象的一个方法,用另一个对象替换当前的对象. 2.用法: call:call(thisObj, ...