uva 270 Lining Up(暴力)

这道题目我是暴力做出来的,复杂度是n^3,因为数组做多有700组,大约可以用这个复杂度,虽然严格来说500多才

是正常的,每次都是选择两个坐标然后确定一条直线,然后遍历一下其他点,用叉积形式判一下是否在一条直线上就ok

啦,网上说可以用极角排序来解,复杂度是n^2logn然而我看了看并没有想学的欲望。。。以后再学吧,,,还用到了

sscanf函数,看别人这样用的,就是在一个数组里匹配想要的类型。。。这题比较坑的就是输入了。每两个输出之间

有空行,最后一组后面不能加空行。。。而且坐标输入之前有空行。。。

贴代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node
{
	int i,j;
}a[705];
int cmp(const void *a,const void *b)
{
	if(((node *)a)->i != ((node *)b)->i)
		return ((node *)a)->i - ((node *)b)->i;
	return ((node *)a)->j - ((node *)b)->j;
}
int main()
{
	int T,i,j,k;
	cin >> T;
	getchar();
	getchar();
	while(T--)
	{
		int i = 1;
		char b[100];
		int n = 1;
		while(gets(b)!=NULL)
		{
			if(b[0] == '\0') break;
            sscanf(b,"%d %d",&a[n].i,&a[n].j);
            n++;
		}
		n = n-1;
		qsort(a+1,n,sizeof(a[0]),cmp);
		int max = 2;

		for(i=1; i<=n; i++)
		{
			for(j=i+1; j<=n; j++)
			{
				int sum = 2;
				for(k=j+1; k<=n; k++)
				{
					if((a[j].j-a[i].j)*(a[k].i-a[j].i) == (a[k].j-a[j].j)*(a[j].i-a[i].i))
					{
						sum++;
					}
				}
				if(sum > max)
					max = sum;
			}
		}
		cout << max << endl;
		if(T != 0)
		cout << endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 17:33:59

uva 270 Lining Up(暴力)的相关文章

uva 270 Lining Up (几何)

uva 270 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 a

UVa 270 - Lining Up

题目:给你平面上n个点,求最多有几个点共线. 分析:计算几何.枚举基准点,其他点按基准点极角排序(斜率排序),然后枚举相邻直线判断统计即可. 说明:时间复杂度O(n*n*lg(n)). #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <cmath> using namesp

uva 467 - Synching Signals(暴力+数学)

题目连接:uva 467 - Synching Signals 题目大意:有n个红绿灯,给出红灯的时间t,那么该灯从0时刻开始就以2*t为周期绿黄红三灯交替,时间分别为t-5,5,t.问所这n个等从第一变为有一个灯不为绿灯开始,要多久才能变成所有的灯刚好都为绿灯.时间超过1小时输出unable to synch after one hour. 解题思路:一小时才3600秒,枚举秒数判断即可. #include <cstdio> #include <cstring> #include

uva 618 - Doing Windows(暴力+数学)

题目链接:uva 618 - Doing Windows 题目大意:给出电脑桌面的大小W和H,现在在桌面上有4个窗口,给出窗口的初始大小,问说能不能通过调整各个窗口的大小(长宽比例不能变)使得4个屏幕刚好占满整个屏幕,并且互相不覆盖. 解题思路:其实可以直接暴力出所有情况,不过细节比较多,而且要考虑所有的细节. 我的做法的是先将4个窗口缩小至最小的状态,然后枚举左下角的窗口, 有四种可能 蓝色部分为另外枚举的窗口,3,4种情况要分别保证说长.宽相等,然后S部分就是子问题. 所以用一个二进制数来表

uva 565 - Pizza Anyone?(暴力枚举 + 二进制)

题目:uva 565 - Pizza Anyone?(暴力枚举 + 二进制) 题目大意:题目是说有一个人要帮他的朋友们定批萨,然后每个朋友都有自己的口味要求,问能不能定一个批萨然后满足每个朋友的至少一个要求. 能就输出所定批萨里面加的东西,,输出要求按字典序: 不能就输出:No pizza can satisfy these requests. 解题思路:这题里面有16种材料,每种材料只有取与不取的可能,这样就有 2^16 种( 0 - 2^16 - 1),枚举出每种情况然后在分别看是否能满足每

uva 1549 - Lattice Point(暴力)

题目链接:uva 1549 - Lattice Point 题目大意:给定圆半径,以原点为圆心,求园内有多少个整数点. 解题思路:首先坐标轴将圆分成4份,所以只要单独考虑每一块的个数乘4再加1即可(原点) #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const double pi = 4 * atan(1.0);

uva 12009 - Avaricious Maryanna(暴力)

题目连接:uva 12009 - Avaricious Maryanna 题目大意:给定n,求x,x为n位数,并且x*x的后n位还是x. 解题思路:打个表会发现其实有规律,除了n=1的时候多了0和1,其他都是n-1位的基础上再新增一位数,1位的时候是5,6. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 500; int a[

UVA 10010-- Where&#39;s Waldorf?--暴力串处理

 Where's Waldorf?  Given a m by n grid of letters, ( ), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid

uva 725 Division(暴力枚举)

uva 725  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =