UVa 10167 Birthday Cake (白皮书第七章 生日蛋糕)

来源:互联网 发布:域名生成二维码 编辑:程序博客网 时间:2024/04/30 13:52
// Birthday Cake (生日蛋糕)

/*

题目地址 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1108

题意:一个蛋糕上有2*N个樱桃,将蛋糕切成相同大小的两部分,且经过中点(0,0),这条直线方程为:
;Ax+By=0;求出其中A,B的值,输出一组解即可。


暴力方法枚举的直线是否能将樱桃分成两个相等的两份了,也就是看点在直线的左侧还是右侧,


还是在直线上了(这里必须考虑其中两种,如果只判断在左侧的樱桃个数是否等于N的话可能会错误,因为有些樱桃可能会在直线上)。


判断点在直线左侧,右侧,直线上的方法。


点(x0,y0):
当x0 <a*y0+b的时候,点在直线左侧
x0> a*y0+b的时候,点在直线右侧
x0=a*y0+b的时候,点在直线上
*/
#include <iostream>
#include <cmath>
using namespace std;
#define MAXN 100


struct point
{
int x;
int y;
};
int main()
{
int N;
point cherries[MAXN + 10];
while (cin >> N, N)
{
for (int i = 1; i <= 2 * N; i++)
cin >> cherries[i].x >> cherries[i].y;
for (int i = -100; i <= 100; i++)
{
bool found = false;
for (int j = -100; j <= 100; j++)
{
int left_num= 0, down_num = 0;
bool online = false;
for (int k = 1; k <= 2 * N; k++)
{
if (i * cherries[k].x + j * cherries[k].y == 0)
{
online = true;
break;
}
else if (i * cherries[k].x + j * cherries[k].y > 0)
left_num++;
else
down_num++;
}


if (online == false && left_num == down_num)
{
cout << i << " " << j << endl;
found = true;
break;
}
}
if (found)
break;
}
}


return 0;
}
0 0
原创粉丝点击