UVa 10167 - Birthday Cake
题目大意:有一个圆形的蛋糕,圆心位于原点,上面有2n颗草莓,要求切一刀把蛋糕分成相等的两分并且每份上都有n颗草莓,问如何切。
暴力枚举即可。如果想优化,可以用一个vis数组避免重复枚举相同的直线,x+y=0和2x+2y=0是相同的直线。
1 #include <cstdio> 2 #define POINTN 110 3 4 struct Point 5 { 6 int x, y; 7 }; 8 Point point[POINTN]; 9 int n; 10 11 bool judge(int A, int B) 12 { 13 int pos = 0, neg = 0; 14 for(int i = 0; i < 2*n; i++) 15 { 16 int res = A * point[i].x + B * point[i].y; 17 if(res == 0) return false; 18 else if(res > 0) pos++; 19 else if(res < 0) neg++; 20 } 21 if(pos == neg) return true; 22 return false; 23 } 24 25 int main() 26 { 27 #ifdef LOCAL 28 freopen("in", "r", stdin); 29 #endif 30 while(scanf("%d", &n) != EOF && n) 31 { 32 for(int i = 0; i < 2*n; i++) 33 scanf("%d%d", &point[i].x, &point[i].y); 34 int A, B; 35 for(A = -500; A <= 500; A++) 36 for(B = 0; B <= 500; B++) 37 if(judge(A, B)) 38 goto found; 39 found: printf("%d %d\n", A, B); 40 } 41 return 0; 42 }