24h購物| | PChome| 登入
2012-04-21 11:53:48| 人氣1,662| 回應0 | 上一篇 | 下一篇

[UVA][Random] 10167 - Birthday Cake

推薦 0 收藏 0 轉貼0 訂閱站台


 Problem G. Birthday Cake 

Background

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 in the range [-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


隨機抓 A、B, 只要有符合就行了, 由於解太多, 迭代跑起來就會慢了點

#include <stdio.h>
int x[100], y[100];
int test(int A, int B, int N) {
static int i, pos, neg, tmp;
pos = 0, neg = 0;
for(i = 2*N-1; i >= 0; i--) {
tmp = A*x[i] + B*y[i];
if(tmp < 0)
neg ++;
else if(tmp > 0)
pos ++;
else
return 0;
}
return pos == neg;
}
void find(int N) {
int A, B;
while(1) {
A = rand()%1001-500;
B = rand()%1001-500;
if(test(A, B, N)) {
printf("%d %d\n", A, B);
break;
}
}
}
int main() {
int N, i;
while(scanf("%d", &N) == 1 && N) {
for(i = 2*N-1; i >= 0; i--)
scanf("%d %d", &x[i], &y[i]);
find(N);
}
return 0;
}
 

台長: Morris
人氣(1,662) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 11470 - Square Sums
此分類上一篇:[UVA] 11879 - Multiple of 17

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文