HDOJ(HDU) 2161 Primes(素数打表)

简介: 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;
                }
            }
        }
    }
}
目录
相关文章
畅通工程 HDU - 1232
畅通工程 HDU - 1232
89 0
|
人工智能 Java
2021杭电多校5-Arrary-hdu7020
Array Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 965 Accepted Submission(s): 312 Problem Description Given an integer array a[1…n].
188 0
2021杭电多校5-Arrary-hdu7020
|
算法 Java
HDU 2084 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
187 0
|
Java 人工智能 Windows
|
测试技术 Java
hdu 1892 See you~
点击打开hdu 1892 思路: 二维树状数组 分析: 1 题目给定4种操作:  S x1 y1 x2 y2 询问以(x1 , y1) - (x2 , y2)为对角线的矩形的面积,但是这个对角线不一定是正对角线。
1023 0
|
存储
hdu 2203 亲和串
点击打开链接hdu 2203 思路:kmp 分析: 1 题目要求的是给定字符串s1 和 s2,问s1能否通过移位得到使得s2包含在s1里面。
826 0