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

// 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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章