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;
                }
            }
        }
    }
}
时间: 2024-10-23 16:30:54

HDOJ(HDU) 2161 Primes(素数打表)的相关文章

HDOJ 2098 分拆素数和

Problem Description 把一个偶数拆成两个不同素数的和,有几种拆法呢? Input 输入包含一些正的偶数,其值不会超过10000,个数不会超过500,若遇0,则结束. Output 对应每个偶数,输出其拆成不同素数的个数,每个结果占一行. Sample Input 30 26 0 Sample Output 3 2 素数打表法! import java.util.Scanner; public class Main { static int[] a = new int[10001

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

HDOJ/HDU 2710 Max Factor(素数快速筛选~)

Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as be

HDOJ/HDU 2551 竹青遍野(打表~)

Problem Description "临流揽镜曳双魂 落红逐青裙 依稀往梦幻如真 泪湿千里云" 在MCA山上,除了住着众多武林豪侠之外,还生活着一个低调的世外高人,他本名逐青裙,因为经常被人叫做"竹蜻蜓",终改名逐青,常年隐居于山中,不再见外人.根据山上附近居民所流传的说法,逐青有一个很奇怪的癖好,从他住进来那天开始,他就开始在他的院子周围种竹子,第1个月种1根竹子,第2个月种8根竹子,第3个月种27根竹子-第N个月就种(N^3)根竹子.他说当他种下第X根竹子那

POJ 2739 素数打表

题意:给你一个数问你用连续的素数来表示这个数有几种表示方法. 先打一个素数表,再把素数表变下形,prime[i]存prime[i]之前的素数和,循环一下打出ans表即可. #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxn 10050 int prime[maxn],nprime,ans[max

[LeetCode] Count Primes - 素数系列问题

题目概述: Description:Count the number of prime numbers less than a non-negative number, n. 解题方法: 题意是给出n中所有素数的个数. 首先你需要知道判断一个数是不是素数的方法:(最笨方法但有效) bool IsPrime(int n) { if (n<2) { //小于2的数即不是合数也不是素数 return false; } //和比它小的所有的数相除,如果都除不尽则证明素数 for(int i=2; i<

HDOJ/HDU 1161 Eddy&amp;#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2061 Treasure the new start, freshmen!(水题、)

Problem Description background: A new semester comes , and the HDU also meets its 50th birthday. No matter what's your major, the only thing I want to tell you is:"Treasure the college life and seize the time." Most people thought that the colle