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

Re:用JAVA的帮看看,WHY RE?要疯了

Posted by gostopsb at 2007-01-02 22:15:55 on Problem 1595
In Reply To:用JAVA的帮看看,WHY RE?要疯了 Posted by:gostopsb at 2007-01-02 22:15:29
> import java.util.*;
> public class P1595
> {
>     private static final int MAX = 3000;
> 	public static boolean isPrime(int n)
>     {
>     	for(int i=2;i<Math.sqrt((double)n)+1;i++)
>     	{
>     		if(n%i==0)
>     		{
>     			return false;
>     		}
>     	}
>     	return true;
>     }
> 	/**
> 	 * @param args
> 	 */
> 	public static void main(String[] args)
> 	{
> 		// TODO 自动生成方法存根
>         Scanner sc = new Scanner(System.in);
>         
>         boolean[] primeList=new boolean[MAX];
>         
>        ///////////////建立质数表
>         primeList[1]=true;
>         primeList[2]=true;
>         for(int i=3;i<MAX;i++)
>         {
>         	if(isPrime(i))
>         	{
>         		primeList[i]=true;
>         	}
>         }
>         ////////////建立质数表
>         
>         /////////建立从1到N之间质数个数表
>         int[] count=new int[MAX];
>         
>         count[1]=1;
>         count[2]=2;
>         
>         for(int i=3;i<MAX;i++)
>         {
>         	if(isPrime(i))
>         	{
>         		count[i]=count[i-1]+1;
>         	}
>         	else
>         	{
>         		count[i]=count[i-1];
>         	}
>         }
>         /////////建立从1到N之间质数个数表
>         
>         int n=sc.nextInt();
>         int c=sc.nextInt();
>         
>         LinkedList<Integer> result=new LinkedList<Integer>();
>         int index=0;
>         while(true)
>         {
>         	index=0;
>         	result.clear();
>         	int cc=c;
>         	if(count[n]%2==0)
>         	{
>         		cc=2*c;
>         	}
>         	else
>         	{
>         		cc=2*c-1;
>         	}
>         	
>         	for(int i=1;index<n&&i<=n;i++)
>         	{
>         		if(primeList[i])
>         		{
>         			result.add(i);
>         			index++;
>         		}
>         	}
>         	if(cc>count[n])
>         	{
>         		cc=count[n];
>         	}
>         	
>         	
>         	System.out.print(n+" "+c+":");
>         	
>         	for(int i=(count[n]-cc)/2;i<(count[n]-cc)/2+cc;i++)
>         	{
>         		
>         		System.out.print(" "+result.get(i));
>         	}
>         	System.out.println();
>         	System.out.println();
>         	n=sc.nextInt();
>         	c=sc.nextInt();
>         	
>         }
>         
>         
> 	}
> 
> }

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