九度 题目1548:平面上的点

题目描述:

给定平面上的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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值