原題:
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 mustn’t in [−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
大意:
給你一個直徑是100的蛋糕,上面有2*n個草莓。現在讓你找出A和B使得沿著直線AX+BY=0切割能使得兩半的蛋糕上面有數目相同的草莓。(不會有兩個草莓在一條直線上)
#include <bits/stdc++.h>using namespace std;//fstream in,out;struct point{ int x,y;};point p[101];int main(){ ios::sync_with_stdio(false); int n; while(cin>>n,n) { for(int i=1;i<=n*2;i++) cin>>p[i].x>>p[i].y; int flag=0,mark=0; int cnt=0,a,b; for(int A=-500;A<=500;++A) { for(int B=-500;B<=500;++B) { mark=cnt=0; int i; if(A==0&&B==0) continue; for(i=1;i<=n*2;i++) { if(A*p[i].x+B*p[i].y==0) { mark=1; break; } if(A*p[i].x+B*p[i].y<0) cnt++; } if(cnt==n&&mark==0) { a=A; b=B; flag=1; break; } } if(flag) break; } cout<<a<<" "<<b<<endl; } return 0;}
解答:
感覺自己代碼能力比較粗糙,就找了點暴力求解的問題去做。這題剛拿到的時候沒仔細看資料,大概明白了什麼意思就開始做,結果想了好久也沒想出來。後來看別人的報告,發現A和B的範圍是[-500,500]而且不用考慮小數的問題。 那麼這道題就相當簡單了,直接枚舉每個A和B就行了