题解:由于解太多,随机抓 A、B, 只要有符合就行了; (首先,Ax+By=0必须表示直线,即A、B不能同时为0;另外,要注意到直线不能过输入中的2N个点;检测点在直线的哪一侧,只需要简单的线性规划的知识)
#include <cstdio>
#include <cstdlib> int x[], y[]; int test(int A, int B, int N)
{
static int i, pos, neg, tmp;
pos = , neg = ;
for (i = *N-; i >= ; i--)
{
tmp = A*x[i] + B*y[i];
if (tmp > ) neg ++;
else if(tmp < ) pos ++;
else return ;
}
return pos == neg;
} void find(int N)
{
int A, B;
while()
{
A = rand()% - ;
B = rand()% - ;
if(test(A, B, N))
{
printf("%d %d\n", A, B);
break;
}
}
} int main()
{
int N, i;
while(scanf("%d", &N) == && N)
{
for (i = *N-; i >= ; i--)
scanf("%d %d", &x[i], &y[i]);
find(N);
}
}
枚举:
#include<iostream>
#include<vector>
using namespace std;
void bruteforce(int &A, int &B, vector<int> &x, vector<int> &y, int n){
for (A = -; A <= ; A++){
for (B = -; B <= ; B++){
int d = , u = ;
for (int i = ; i < * n; i++){
if (A*x[i] + B*y[i] > ) u++;
if (A*x[i] + B*y[i] < ) d++;
}
if (u == n&&d == n) return;
}
}
}
int main()
{
int n;
while (cin >> n&&n != ){
vector<int> x(*n, );
vector<int> y(*n, );
for (int i = ; i < *n; i++){
cin >> x[i] >> y[i];
}
int A, B;
bruteforce(A, B, x, y, n);
cout << A << ' ' << B << endl;
}
return ;
}