题意:给出一些点的坐标,输出最多有几个点在一条直线上。
题解:因为没说是几个点,而是由换行控制数量,所以要用字符串方式读入,刚开始很辛苦的把数字分割,判断是否有负号,写完发现别人都用了sscanf()这个函数,好方便,果断改掉自己写的乱七八糟的代码,然后是根据两个点确定一条直线,根据差积相等公式判断n个点中有多少点在这条直线上,将最大值更新。
#include <stdio.h>
#include <string.h>
const int N = 705;
int point[N][2], n;
char str[30];
void solve() {
int ans = -1;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
int temp = 0;
for (int k = 0; k < n; k++)
if ((point[i][1] - point[j][1]) * (point[i][0] - point[k][0]) == (point[i][1] - point[k][1]) * (point[i][0] - point[j][0]))
temp++;
if (temp > ans)
ans = temp;
}
printf("%d\n", ans);
}
int main() {
int t;
scanf("%d", &t);
getchar();
getchar();
while (t--) {
n = 0;
while (gets(str) && str[0] != '\0') {
sscanf(str, "%d%d", &point[n][0], &point[n][1]);
n++;
}
solve();
if (t)
printf("\n");
}
return 0;
}