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<stdio.h> #include<stdlib.h> int a,b,n; struct coor{ int x; int y; }c[105]; bool check(int A,int B){ int num=0; for(int i=0;i<2*n;i++){ if(A*c[i].x+B*c[i].y==0)return false; if(A*c[i].x+B*c[i].y>0)num++; } if(num==n){ a=A,b=B; return true; } return false; } int main(){ bool flag; while(scanf("%d",&n)==1&&n){ flag=true; for(int i=0;i<2*n;i++) scanf("%d %d",&c[i].x,&c[i].y); for(int i=-500;i<=500&&flag;i++){ for(int j=-500;j<=500&&flag;j++){ if(i<0&&j<0)continue; if(check(i,j)) flag=false; } } printf("%d %d\n",a,b); } return 0; }