Description
一天,小希坐在院子里数星星,Gardon就出了个难题给她,让她数数天上的星星最多有多少个是在同一条直线上的。天上的星星太多了,小希马上就看花了眼,你能写个程序来帮她计算么?
Input
输入包含多组数据,每组数据的开头是一个整数N(N<=300),接下来的N对数每对表示一个星星的位置(星星的坐标在-10000到10000之间,精确到小数点后1位)。没有两个星星会在同一个位置。
Output
一个整数,表示一条直线上最多星星的数目。
Sample Input
5
0 0
1 0
1 1
0 1
0.5 0.5
Sample Output
3
#include <iostream>
using namespace std;
int maxnum(double r[], int t);
int main()
{
int n, i, j, max, num, t;
double a[400][3];
double b[400];
while (cin>>n)
{
max = 0;
for (i = 0; i < n; i++)
{
cin>>a[i][0]>>a[i][1];
}
if (n == 0 || n == 1 || n == 2)
{
cout<<n<<endl;
continue;
}
for (i = 0; i < n; i++)
{
num = 0;
for (j = i + 1; j < n; j++)
{
if (a[i][0] == a[j][0])
{
b[num++] = 100000.0;
}
else b[num++] = (a[i][1] - a[j][1]) / (a[i][0] - a[j][0]);
}
t = maxnum(b, num);
if (max < t)
{
max = t;
}
if (max > n / 2)
{
break;
}
}
cout<<max+1<<endl;
}
return 0;
}
int maxnum(double r[], int t)
{
int i, j, count, temp;
temp = 0;
for (i = 0; i < t - 1; i++)
{
count = 1;
for (j = i + 1; j < t ; j++)
{
if ((r[i] - r[j] <= 0.00001)&&(r[i] - r[j] >= -0.00001))
{
count++;
}
}
if (temp < count)
{
temp = count;
}
}
return temp;
}
谁能帮我看看错在哪不?