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-12-30 01:42:52

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

hdu 2161 Primes 素数打表

在kuangbin带你飞专题看到的,水了一发,但是wa了一次,T了一次,竟然连素数打表都快不会写了. 而且连求素数时候只需到根号n就可以都忘了,假设有因子m大于√n,那么n/m一定小于√n,所以它在√n前面已经被选出来了. 代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<vector>

HDU 2161 Primes(打表)

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9888    Accepted Submission(s): 4136 Problem Description Write a program to read in a list of integers and determine whether or not each n

HDU 2161 Primes (素数筛选法)

题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long LL; const int maxn = 16000 + 10; int p

hdu 2161 Primes 筛法求素数 大水题

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7659    Accepted Submission(s): 3130 Problem Description Write a program to read in a list of integers and determine whether or not each n

HDU 2161: Primes

Primes ///@author Sycamore, ZJNU ///@date 8/2/2017 #include<iostream> #include <cmath> #include<set> #include<algorithm> using namespace std; set<int>s = {3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,1

hdu 2521 反素数(打表)

反素数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5723    Accepted Submission(s): 3355 Problem Description 反素数就是满足对于任意i(0<i<x),都有g(i)<g(x),(g(x)是x的因子个数),则x为一个反素数.现在给你一个整数区间[a,b],请你求出该区间的x使

HDOJ(HDU) 2521 反素数(因子个数~)

Problem Description 反素数就是满足对于任意i(0< i < x),都有g(i) < g(x),(g(x)是x的因子个数),则x为一个反素数.现在给你一个整数区间[a,b],请你求出该区间的x使g(x)最大. Input 第一行输入n,接下来n行测试数据 输入包括a,b, 1<=a<=b<=5000,表示闭区间[a,b]. Output 输出为一个整数,为该区间因子最多的数.如果满足条件有多个,则输出其中最小的数. Sample Input 3 2 3

UVA1213Sum of Different Primes(素数打表 + DP)

题目链接 题意:选择k个素数,使得和为N(1120)的方案数: 筛选出 <= N 的素数,然后就背包 写的时候没初始dp[0][0] = 1;而且方案数也没相加,真是弱逼 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int Max = 1120; int prime[Max + 5], t

HDU - 2161 - Primes (质数)

Primes Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8959    Accepted Submission(s): 3754 Problem Description Write a program to read in a list of integers and determine whether or not each n