题目就是问如何用一条直线切蛋糕让直线两侧樱桃数量相等
AX+BY=0 是直线 那么只要判断 AX1+BY1>0 与AX1+BY1<0 的点数量是否相等,并且没有等于0的情况
题目:
Problem G. Birthday Cake |
Background
Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them.Now we put the cake onto a Descartes coordinate. Its center is at (0,0), and the cake's length of radius is 100.
There are 2N (N is a integer, 1<=N<=50) cherries on the cake. Mother wants to cut the cake into two halves with a knife (of course a beeline). The twins would like to be treated fairly, that means, the shape of the two halves must be the same (that means the beeline must go through the center of the cake) , and each half must have N cherrie(s). Can you help her?
Note: the coordinate of a cherry (x , y) are two integers. You must give the line as form two integers A,B(stands for Ax+By=0), each number in the range [-500,500]. Cherries are not allowed lying on the beeline. For each dataset there is at least one solution.
Input
The input file contains several scenarios. Each of them consists of 2 parts: The first part consists of a line with a number N, the second part consists of 2N lines, each line has two number, meaning (x,y) .There is only one space between two border numbers. The input file is ended with N=0.
Output
For each scenario, print a line containing two numbers A and B. There should be a space between them. If there are many solutions, you can only print one of them.
Sample Input
2 -20 20 -30 20 -10 -50 10 -5 0
Sample Output
0 1
代码:
#include <iostream> using namespace std; int X[200]; int Y[200]; int main() { int N; while(cin>>N &&N!=0) { for(int i=0;i<2*N;i++) { cin>>X[i]>>Y[i]; } for(int A=-500;A<=500;A++) { int neg=0,pos=0,zero=0,flag=0; for(int B=-500;B<=500;B++) { if(A==0&&B==0)continue; for(int i=0;i<2*N;i++) { if(A*X[i]+B*Y[i]>0)pos++; if(A*X[i]+B*Y[i]<0)neg++; if(A*X[i]+B*Y[i]==0){zero++;break;} } // cout<<"POS"<<pos<<" NEG"<<neg<<"zeor"<<zero<<endl; if(pos==neg && zero==0) { cout<<A<<" "<<B<<endl;flag=1; break; } pos=neg=zero=0; } if(flag)break; } } return 0; }