- 题目描述:
-
给定平面上的n个点,任意做一条直线,求至多能有几个点恰好落在直线上。
- 输入:
-
包含多组测试数据,每组测试数据由一个整数n(0<=n<=100)开始,代表平面上点的个数。
接下去n行每行给出一个点的坐标(x,y),x、y的绝对值均小于等于100。
- 输出:
-
对于每组测试数据,输出一个整数,表示至多能有几个点恰好落在直线上。
- 样例输入:
-
2 0 0 1 1 4 0 0 1 1 2 2 3 6
- 样例输出:
-
2 3
/*
* dot.cpp
*
* Created on: 2014年12月31日
* Author: judyge
*/
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<time.h>
#define random(x) (rand()%x)
using namespace std;
bool randbool(){
bool flag;
int intflag=random(2);
if(intflag==0){flag=false;}
else{flag=true;}
return flag;
}
float k(float x1,float y1,float x2,float y2)
{
float ck;
if(x2-x1==0){ck=10000.0000;}
else{ck=(y2-y1)/(x2-x1);}
return ck;
}
void randxy(float *nx,float *ny)
{
srand((unsigned)time(0));
for(int x=0;x<100;x++)
{
if(randbool())
{nx[x]=random(100)/1.0000;}
else
{nx[x]=-random(100)/1.0000;}
}
for(int y=0;y<100;y++)
{
if(randbool())
{ny[y]=random(100)/1.0000;}
else
{ny[y]=-random(100)/1.0000;}
}
}
int countk(float *nx1,float *ny1)
{
int count=0;
int tmpcount;
for(int i=0;i<100;i++)
{
float mk=k(nx1[i],ny1[i],nx1[i+1],ny1[i+1]);
tmpcount=0;
for(int j=i+1;j<100;j++)
{
if(k(nx1[i],ny1[i],nx1[j],ny1[j])==mk)
{
tmpcount++;
}
}
if(tmpcount>count)
{
count=tmpcount;
}
}
return count;
}
int main()
{
float nx1[100];
float ny1[100];
randxy(nx1,ny1);
// for(int i=0;i<100;i++)
// {
// cout<<nx1[i]<<','<<ny1[i]<<'\n';
// }
cout<<countk(nx1,ny1);
return 0;
}
运行结果
2start:1 finish:1 finish-start:0 runtime:0.000000