Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

POJ3987 Java

Posted by licb1964 at 2019-09-19 15:56:36 on Problem 3978
import java.util.Scanner; 
import java.util.*;
public class Main
{
	static int MAXN=100010;					//最大整数
	static int[] sum=new int[MAXN];
	static void Primes()					//求解算法
	{
		Arrays.fill(sum,0);
		sum[0]=sum[1]=1;
		for(int i=2;i<Math.sqrt((double)MAXN);i++)
		{
			if(sum[i]==0)                 	//若i是素数
			{   for(int j=i*i;j<MAXN;j+=i) 	//整除的肯定不是素数
				{
					if (j>MAXN) System.out.println("j>MAXN");
					sum[j]=1;
				}
			}
		}
		sum[0]=0;
		for(int i=1;i<MAXN;i++)
		{
			if(sum[i]>0)					//若i不是素数
				sum[i]=sum[i-1];
			else							//若i是素数
				sum[i]=sum[i-1]+1;
		}
	}
    public static void main(String[] args)
	{
		Scanner fin=new Scanner(System.in);
		Primes();
		int a,b,ans;
		while(fin.hasNext())
		{
			a=fin.nextInt();
			b=fin.nextInt();
			if (a==-1 && b==-1)
				break;
			ans=0;
			if (a>=1) ans=sum[b]-sum[a-1];
			else ans=sum[b]-sum[0];	
			System.out.println(ans);
		}
	}
}
以上程序本地运行正确,为什么POJ中Rumtime error,请大家帮助啊!

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator