首页 > 专题系列 > Java解POJ > POJ 1118 Lining Up [解题报告] Java
2013
11-09

POJ 1118 Lining Up [解题报告] Java

Lining Up

问题描述 :

“How am I ever going to solve this problem?” said the pilot.

Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and she had to fly over as many points as possible. All points were given by means of integer coordinates in a two-dimensional space. The pilot wanted to know the largest number of points from the given set that all lie on one line. Can you write a program that calculates this number?

Your program has to be efficient!

输入:

Input consist several case,First line of the each case is an integer N ( 1 < N < 700 ),then follow N pairs of integers. Each pair of integers is separated by one blank and ended by a new-line character. The input ended by N=0.

输出:

output one integer for each input case ,representing the largest number of points that all lie on one line.

样例输入:

5
1 1
2 2
3 3
9 10
10 11
0

样例输出:

3

解题代码:

//* @author: [email protected]
import java.util.*;
public class Main
{
 public static void main(String[] args)
 {
  Scanner in=new Scanner(System.in);
  while(true)
  {	
   int a=in.nextInt();
   if(a==0)break;
	int[] arrx=new int[a];
	int[] arry=new int[a];
	for(int i=0;i< a;i++)
	{
		arrx[i]=in.nextInt();
		arry[i]=in.nextInt();
	}
	int max=0;
	for(int i=0;i< a-2;i++)
	{
		for(int j=i+1;j< a-1;j++)
		{
			int c=0;
			for(int u=j+1;u< a;u++)
			{
	if((arrx[i]-arrx[u])*(arry[i]-arry[j])==(arrx[i]-arrx[j])*(arry[i]-arry[u]))c++;
			}
			if(c>max)max=c;
		}
	}
	System.out.println(max+2);
    }
  }
}

  1. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])

  2. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

  3. 换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。
    应该是,不可能小于合并后的第K小值吧