题目大意:给定一些点集,要你找两点之间的连线不平行的有多少条
数据量比较少,直接暴力枚举,然后放到set查找即可
1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 #include <set> 5 6 using namespace std; 7 8 static struct _p_set 9 { 10 long double x, y; 11 }points[201]; 12 set<long double>lines; 13 14 int gcd(const int, const int); 15 16 int main(void) 17 { 18 int point_sum, cut; 19 long double tmp; 20 //pair<int,int>tmp; 21 while (~scanf("%d", &point_sum)) 22 { 23 lines.clear(); 24 for (int i = 0; i < point_sum; i++) 25 scanf("%lf%lf", &points[i].x, &points[i].y); 26 for (int i = 0; i < point_sum; i++) 27 { 28 for (int j = i + 1; j < point_sum; j++) 29 { 30 if ((points[i].x - points[j].x) != 0) 31 tmp = (points[i].y - points[j].y) / (points[i].x - points[j].x); 32 else 33 tmp = (long double)INT_MAX; 34 lines.insert(tmp); 35 } 36 } 37 printf("%d ", lines.size()); 38 } 39 40 return EXIT_SUCCESS; 41 }
在讨论版那里还找到了一种很新奇的做法,可以无视除数是0和精度的问题
1 #include <iostream> 2 #include <functional> 3 #include <algorithm> 4 #include <set> 5 6 using namespace std; 7 8 static struct _p_set 9 { 10 int x, y; 11 }points[201]; 12 set<pair<int,int>>lines; 13 14 int gcd(const int, const int); 15 16 int main(void) 17 { 18 int point_sum, cut; 19 pair<int,int>tmp; 20 while (~scanf("%d", &point_sum)) 21 { 22 lines.clear(); 23 for (int i = 0; i < point_sum; i++) 24 scanf("%d%d", &points[i].x, &points[i].y); 25 for (int i = 0; i < point_sum; i++) 26 { 27 for (int j = i + 1; j < point_sum; j++) 28 { 29 cut = gcd(points[i].y - points[j].y, points[i].x - points[j].x); 30 tmp.first = (points[i].y - points[j].y) / cut; 31 tmp.second = (points[i].x - points[j].x) / cut; 32 lines.insert(tmp); 33 } 34 } 35 printf("%d ", lines.size()); 36 } 37 return EXIT_SUCCESS; 38 } 39 40 int gcd(const int a, const int b) 41 { 42 if (b == 0) 43 return a; 44 return gcd(b, a%b); 45 }
其实时间差不多