uva 270 Lining Up(暴力+ 几何)
2013-08-24 13:55
267 查看
题目连接:270 - Lining Up
题目大意:给出一系列点,要求找出共线的点数最大的值。
解题思路:暴力,遍历每两个点确定的直线上又多少点, 每次更新最大值。
三个点共线的性质:A(X1,Y1),B(X2,Y2),C(X3,Y3);这个时候有(Y2-Y1)/(X2-X1) = (Y3-Y2)/(X3-X2)
最好转化成(Y2 - Y1) * (X3 - X2) = (Y3 - Y2) * (X2 - X1).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int max(const int &a, const int &b) { return a > b ? a : b;}
const int N = 1005;
int n, x
, y
;
int find (int p, int q, int c) {
int cnt = 0;
for (int i = c + 1; i < n; i++) {
if (p * (y[i] - y[c]) == q * (x[i] - x[c]))
cnt++;
}
return cnt;
}
int solve() {
int Max = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int cur = find(x[j] - x[i], y[j] - y[i], j) + 2;
Max = max(Max, cur);
}
}
return Max;
}
int main() {
int cas;
char str
;
scanf("%d%*c%*c", &cas);
while (cas--) {
// Init;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
n = 0;
// Read;
while (gets(str)) {
if (!str[0] && n) break;
sscanf(str, "%d%d", &x
, &y
);
n++;
}
printf("%d\n", solve());
if (cas) printf("\n");
}
return 0;
}
题目大意:给出一系列点,要求找出共线的点数最大的值。
解题思路:暴力,遍历每两个点确定的直线上又多少点, 每次更新最大值。
三个点共线的性质:A(X1,Y1),B(X2,Y2),C(X3,Y3);这个时候有(Y2-Y1)/(X2-X1) = (Y3-Y2)/(X3-X2)
最好转化成(Y2 - Y1) * (X3 - X2) = (Y3 - Y2) * (X2 - X1).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int max(const int &a, const int &b) { return a > b ? a : b;}
const int N = 1005;
int n, x
, y
;
int find (int p, int q, int c) {
int cnt = 0;
for (int i = c + 1; i < n; i++) {
if (p * (y[i] - y[c]) == q * (x[i] - x[c]))
cnt++;
}
return cnt;
}
int solve() {
int Max = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int cur = find(x[j] - x[i], y[j] - y[i], j) + 2;
Max = max(Max, cur);
}
}
return Max;
}
int main() {
int cas;
char str
;
scanf("%d%*c%*c", &cas);
while (cas--) {
// Init;
memset(x, 0, sizeof(x));
memset(y, 0, sizeof(y));
n = 0;
// Read;
while (gets(str)) {
if (!str[0] && n) break;
sscanf(str, "%d%d", &x
, &y
);
n++;
}
printf("%d\n", solve());
if (cas) printf("\n");
}
return 0;
}
相关文章推荐
- uva 270 Lining Up (几何)
- uva270 - Lining Up(几何 - 最大共线点)
- UVA 270 Lining Up 共线点 暴力
- uva 270 Lining Up(暴力)
- 计算几何——Uva 270 Lining Up
- uva270 - Lining Up(暴力)
- UVA 270 Lining Up (几何 判断共线点)
- UVa 270 / POJ 1118 Lining Up (计算几何)
- UVA 270 Lining Up (几何 判断共线点)
- uva 270 Lining Up(暴力或极角排序)
- UVA 270 Lining Up 共线点 暴力
- uva270 Lining Up
- UVA - 270 Lining Up
- uva270 Lining Up 搜索?
- UVA270- Lining Up
- Uva 270 Lining Up
- UVALive5379 UVA270 Lining Up【输入输出+水题】
- uva 270 Lining Up/poj 1118
- UVA - 270 Lining Up
- UVA 270 Lining Up(多点共线)