uva 10167 Birthday Cake(暴力/枚举)

uva 10167 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

题目大意:有一块蛋糕(圆形),蛋糕上有樱桃,要求切一刀,使得分割线过圆心并且被分割的两块上的樱桃数量一样多。

解题思路:利用关系式 Ax + By = 0,枚举所有可能的 A 和 B,统计在直线上下的樱桃数是否相等。

#include<stdio.h>
struct cherry {
	int x;
	int y;
};
int main() {
	int m;
	while (scanf("%d", &m) == 1, m) {
		cherry c[5500];
		for (int i = 0; i < 2 * m; i++) {
			scanf("%d", &c[i].x);
			scanf("%d", &c[i].y);
		}
		int cnt1, cnt2, i, j;
		for (i = -500; i <= 500; i++) {
			for (j = -500; j <= 500; j++) {
				if (i == 0 && j == 0) {continue;}
				cnt1 = 0; cnt2 = 0;
				for (int k = 0; k < 2 * m; k++) {
					if (i * c[k].x + j * c[k].y < 0) {   //在分割线下方的樱桃
						cnt1++;
						continue;
					}
					else if (i * c[k].x + j * c[k].y > 0) {   //在分割线上方的樱桃
						cnt2++;
						continue;
					}
					else break;   //切到了樱桃,跳出循环
				}

				if (cnt1 == m && cnt2 == m) {  //如果分割线上下的樱桃数相等,且等于总樱桃数的一半,切割成功。
					printf("%d %d\n", i, j);
					break;
				}
			}
			if (cnt1 == m && cnt2 == m) {
				break;
			}
		}
	}
	return 0;
}
时间: 2024-08-27 11:36:06

uva 10167 Birthday Cake(暴力/枚举)的相关文章

uva 10167 Birthday Cake(暴力枚举)

....还不是完全自己独立做出来的题目,虽然很暴力,好像是范围为[-500,500],但是题上为什mustn't in呢,我还白痴的用点到直线的距离求个数,判断是在直线上还是下应该直接带入就ok了!!!看是大于0还是小于0,不过通过这个我又知道了点到直线距离公式,之前给忘了,d = abs(Ax+By+c)/sqrt(A*A+B*B) 贴代码了: #include<stdio.h> #include<string.h> #include<stdlib.h> #inclu

[UVA] 10167 - Birthday Cake

 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

uva 565 - Pizza Anyone?(暴力枚举 + 二进制)

题目:uva 565 - Pizza Anyone?(暴力枚举 + 二进制) 题目大意:题目是说有一个人要帮他的朋友们定批萨,然后每个朋友都有自己的口味要求,问能不能定一个批萨然后满足每个朋友的至少一个要求. 能就输出所定批萨里面加的东西,,输出要求按字典序: 不能就输出:No pizza can satisfy these requests. 解题思路:这题里面有16种材料,每种材料只有取与不取的可能,这样就有 2^16 种( 0 - 2^16 - 1),枚举出每种情况然后在分别看是否能满足每

uva 725 Division(暴力枚举)

uva 725  Division Write a program that finds and displays all pairs of 5-digit numbers that between them use the digits 0 through 9 once each, such that the first number divided by the second is equal to an integer N, where . That is, abcde / fghij =

UVA - 10167 - Birthday Cake (简单枚举)

思路:简单枚举 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> using namespace std; int x[105], y[105]; int main() { int A, B, N; while(scanf("%d", &N), N) { for(int

Brute Force --- UVA 10167: Birthday Cake

 Problem G. Birthday Cake  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=13&problem=1108&mosmsg=Submission+received+with+ID+14413715 Mean: http://luckycat.kshs.kh.edu.tw/

uva 11210 Chinese Mahjong(暴力枚举)

uva 11210 Chinese Mahjong Mahjong () is a game of Chinese origin usually played by four persons with tiles resembling dominoes and bearing various designs, which are drawn and discarded until one player wins with a hand of four combinations of three

UVa 10603 Fill [暴力枚举、路径搜索]

10603 Fill There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The rst and the second jug are initially empty, while the third is completely lled with water. It is allowed to pour water f

UVA 725 division【暴力枚举】

[题意]:输入正整数n,用0~9这10个数字不重复组成两个五位数abcde和fghij,使得abcde/fghij的商为n,按顺序输出所有结果.如果没有找到则输出“There are no solutions for N.”.这里2<=n<=79. [分析]: 1.因为n>=2,且abcde=fghij×n,满足abcde>fghij.若a=0,则fghij的最小值为12345,abcde<fghij,矛盾.所以a≠0. 2.因为a≠0,所以12345<=abcde&l