題意:
給你一系列的點,讓你找出一條直線AX+BY=0, 使得直線把這些點劃分爲兩個部分
分析:
由於題目給出的AB範圍小且爲整數,直接暴力可以過,不過這裏要注意精度問題,判斷是否在直線上不能直接與zero比較
Code:
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define oo (~0u)>>1
#define INF 0x3F3F3F3F
#define REPI(i, s, e) for(int i = s; i <= e; i ++)
#define REPD(i, e, s) for(int i = e; i >= s; i --)
const int MAXN = 100;
const int EPS = 10e-6;
double px[MAXN+1], py[MAXN+1];
void input(int n)
{
for(int i = 1; i <= n; i ++) {
scanf("%lf %lf", &px[i], &py[i]);
}
}
int ok(int a, int b, int n)
{
if( !b ) {
return 0;
}
double k = -a/(double)b;
int up_cnt = 0, down_cnt = 0;
for(int i = 1; i <= n; i ++) {
double c = py[i]-k*px[i];
if( fabs(c-0) >= -EPS && fabs(c-0) <= EPS ) {
return 0;
}
else if( c > 0 ) {
up_cnt += 1;
}
else {
down_cnt += 1;
}
}
return up_cnt == down_cnt;
}
void process(int n)
{
int MIN_VAL = -500, MAX_VAL = 500;
for(int a = MIN_VAL; a <= MAX_VAL; a ++) {
for(int b = MIN_VAL; b <= MAX_VAL; b ++) {
if( ok(a, b, n) ) {
printf("%d %d\n", a, b);
return;
}
}
}
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n;
while( scanf("%d", &n) && n ) {
n *= 2;
input(n);
process(n);
}
return 0;
}