我是靠谱客的博主 雪白缘分,最近开发中收集的这篇文章主要介绍AOJ0009Prime Number Prime Number,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Prime Number

Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5, 7.

Input

Input consists of several datasets. Each dataset has an integer n (n ≤ 999999) in a line.

The number of datasets ≤ 30.

Output

For each dataset, prints the number of prime numbers.

Sample Input

10
3
11

Output for the Sample Input

4
2
5

package 素数;

import java.util.Scanner;

public class AOJ0009_Prime_Number {

	/**
	 * @param args
	 */
	static int prime[] = new int[1000000];
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		getans();
		Scanner cin = new Scanner(System.in);
		while(cin.hasNext())
		{
			int a = cin.nextInt();
			System.out.println(prime[a]);
		}
	}
	private static void getans() {
		
		 //for(int i=0; i<1000000; i++)prime[i] = i;
		 prime[1] = 0;
		 prime[2] = 1;
		 for(int i=2; i<1000000; i++)
		 {
			 if(prime[i] == -1)
			 {
				 prime[i] = prime[i-1];continue;
			 }
			 prime[i] = prime[i-1] + 1;
			 for(int j=i*2; j<1000000; j+=i)
			 {
				 prime[j] = -1;
			 }
		 }
	}

}




最后

以上就是雪白缘分为你收集整理的AOJ0009Prime Number Prime Number的全部内容,希望文章能够帮你解决AOJ0009Prime Number Prime Number所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部