“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 scatteredin a dangerous area. Furthermore, the pilot could only fly over the area once in a straight line, and shehad to fly over as many points as possible. All points were given by means of integer coordinates in atwo-dimensional space. The pilot wanted to know the largest number of points from the given set thatall lie on one line. Can you write a program that calculates this number?
Your program has to be efficient!
Input
The input begins with a single positive integer on a line by itself indicating the number of the casesfollowing, each of them as described below. This line is followed by a blank line, and there is also ablank line between two consecutive inputs.
The input consists of N pairs of integers, where 1 < N < 700. Each pair of integers is separated byone blank and ended by a new-line character. The list of pairs is ended with an end-of-file character.No pair will occur twice.
Output
For each test case, the output must follow the description below. The outputs of two consecutive caseswill be separated by a blank line.
The output consists of one integer representing the largest number of points that all lie on one line.
Sample Input
1
1 1
2 2
3 3
9 10
10 11
Sample Output
3
Regionals 1994 >> North America - East Central NA
问题链接:UVALive5379 UVA270 Lining Up。
题意简述:
输入n,输入n个整数对,即n个坐标点,问最多共线点数是多少。
问题分析:
用暴力法解决这个问题,好在计算规模不算大。
程序说明:
程序中,判断共线时,使用的是乘法,没有用除法,可以保证精确的计算结果。
特别需要说明的是,这个问题虽然与参考链接的题是相同的问题,但是数据输入格式不一样,需要特别处理。程序中编写了函数mygetline()实现读入一行,用以代替C语言的库函数gets()(新的C函数库标准中,已将函数gets()剔除,因为该函数容易引起问题)。函数mygetline()中,需要考虑EOF的情形,这是程序员容易忽略的地方。
另外,程序中还使用了函数sscanf()。
参考链接:POJ1118 HDU1432 Lining Up
AC的C语言程序如下:
/* UVALive5379 UVA270 Lining Up */ #include <stdio.h> #define MAXN 700 struct { int x, y; } p[MAXN]; /* point */ void mygetline(char *pc) { char c; while((c=getchar()) != '\n' && c !=EOF) *pc++ = c; *pc = '\0'; } int main(void) { int t, n, ans, max, i, j, k; char s[128]; scanf("%d", &t); getchar(); getchar(); while(t--) { n = 0; mygetline(s); while(s[0] != '\0') { sscanf(s, "%d%d", &p[n].x, &p[n].y); n++; mygetline(s); } ans = 2; for(i=0; i<n; i++) for(j=i+1; j<n; j++) { max = 2; for(k=j+1; k<n; k++) if ((p[j].x - p[i].x)*(p[k].y - p[j].y) == (p[j].y - p[i].y)*(p[k].x - p[j].x)) max++; if(max > ans) ans = max; } printf("%d\n", ans); if(t) printf("\n"); } return 0; }