首页 > 专题系列 > Java解POJ > POJ 1595 Prime Cuts [解题报告] Java
2013
11-10

POJ 1595 Prime Cuts [解题报告] Java

Prime Cuts

问题描述 :

A prime number is a counting number (1, 2, 3, …) that is evenly divisible only by 1 and itself. In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between (and including) 1 and N. Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.

输入:

Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 <= N <= 1000) is the maximum number in the complete list of prime numbers between 1 and N. The second number (1 <= C <= N) defines the C*2 prime numbers to be printed from the center of the list if the length of the list is even; or the (C*2)-1 numbers to be printed from the center of the list if the length of the list is odd.

输出:

For each input set, you should print the number N beginning in column 1 followed by a space, then by the number C, then by a colon (:), and then by the center numbers from the list of prime numbers as defined above. If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from the center of the list should be preceded by exactly one blank. Each line of output should be followed by a blank line. Hence, your output should follow the exact format shown in the sample output.

样例输入:

21 2
18 2
18 18
100 7

样例输出:

21 2: 5 7 11

18 2: 3 5 7 11

18 18: 1 2 3 5 7 11 13 17

100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67

解题代码:

//* @author: [email protected]
import java.util.Scanner;
public class Main
{
  public static void main(String[] args)
  {
   int[] arr=new int[]{
	1,2,3,5,7,11,13,17,19,23,29,31,37,41,
	43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,
	109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,
	193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,
	277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,
	373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,
	461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,
	569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,
	653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,
	757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,
	859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,
	971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,
	1063,1069,1087,1091,1093,1097
    };
	Scanner in=new Scanner(System.in);
	while(in.hasNext())
	{
	 int a=in.nextInt();
	 int b=in.nextInt();
	 System.out.print(a+" "+b+":");
	 int max=184;
	 int min=0;
	 int mid=92;
	 while(min< max)
	  {
		mid=(max+min)/2;
		if(arr[mid]==a){
			min=mid+1;
			break;
		}
		if(arr[mid]>a)max=mid;
		else if(arr[mid]< a) min=mid+1;
	   }
	  if(min%2==1){
		mid=min/2-b+1;
		max=min/2+b-1;
		if(mid< 0) mid=0;
		if(max>min)max=min-1;
	   }
	   else{
		mid=min/2-b;
		max=min/2+b-1;
		if(mid< 0) mid=0;
		if(max>min) max=min-1;
	  }
	  for(int i=mid;i<=max;i++)
		System.out.print(" "+arr[i]);
	  System.out.println();
	  System.out.println();
	}
  }
}

  1. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish

  2. I go through some of your put up and I uncovered a good deal of expertise from it. Many thanks for posting this sort of exciting posts

  3. 你的理解应该是:即使主持人拿走一个箱子对结果没有影响。这样想,主持人拿走的箱子只是没有影响到你初始选择的那个箱子中有奖品的概率,但是改变了其余两个箱子的概率分布。由 1/3,1/3 变成了 0, 2/3