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 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

Source

2008 “Sunline Cup” National Invitational
Contest - Warm Up

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

const int maxn = 20000;
int prime[20000];

void init() {
	prime[1] = 2;
	for(int i = 2; i < maxn; i ++)
		if(prime[i] == 0) {
			prime[i] = 1;
			for(int j = i * 2; j < maxn; j += i) {
				prime[j] = 2;
			}
		}
	prime[2] = 2;
}

int main() {
	init();
	int cas = 1;
	int a;
	while(scanf("%d", &a) != EOF) {
		if(a == 0) break;

		if(prime[a] == 1) {
			printf("%d: yes\n", cas ++);
		}
		else if(prime[a] == 2) {
			printf("%d: no\n", cas ++);
		}
	}
	return 0;
}
时间: 2024-10-12 14:52:32

HDU - 2161 - Primes (质数)的相关文章

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 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

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 i

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 (素数筛选法)

题意:输入一个数判断是不是素数,并规定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 素数打表

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

HDU 5104 Primes Problem(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104 Problem Description Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n. Input Multiple test cases(less than 100), for each

[LeetCode] Count Primes 质数的个数

Description: Count the number of prime numbers less than a non-negative number, n click to show more hints. References: How Many Primes Are There? Sieve of Eratosthenes Credits:Special thanks to @mithmatt for adding this problem and creating all test

Count Primes ----质数判断

质数的判断 埃拉托斯特尼筛法: 算法的过程如下图所示: 我们从2开始遍历到根号n,先找到第一个质数2,然后将其所有的倍数全部标记出来,然后到下一个质数3,标记其所有倍数,依次类推,直到根号n,此时数组中未被标记的数字就是质数. 对于本题,即可采用上述判断质数的方法.