UVA 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 (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

题意:一对双胞胎吃一个蛋糕,蛋糕上面有2*n个草莓,为了公平起见,妈妈必须使每个人都能分到n个草莓,蛋糕输一个半径为100的圆。

对于切蛋糕的要求是:(1)必须是一条直线。

                   (2)这条直线必须过圆心(也就是直径),其中圆心在原点                          (0,0)上。

      对于这样的一条直线设的方程为Ax+By = 0,题目已经说明A,B都在[-500,500]这个区间内。求符合条件的A B,并输出。(A,B的值并不唯一,输出一个符合条件的就可以了)

#include<iostream>

#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

using namespace std;

struct node
{
    int x;
    int y;
}q[201];

int n;

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n == 0)
        {
            break;
        }
        int m = n;
        n = n* 2;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&q[i].x,&q[i].y);
        }
        int sum1 = 0,sum2 = 0;
        int flag = 0;
        for(int i=-500;i<=500;i++)
        {
            for(int j=-500;j<=500;j++)
            {
                if(i == 0 && j == 0)
                {
                    continue;
                }
                sum1 = 0;
                sum2 = 0;
                for(int k=0;k<2*n;k++)
                {
                    if((i*q[k].x + j*q[k].y) > 0)
                    {
                        sum1++;
                    }
                    else if((i*q[k].x + j*q[k].y) < 0)
                    {
                        sum2++;
                    }
                    else
                    {
                        break;
                    }
                }
                //printf("sum1 = %d      sum2 = %d      m = %d\n",sum1,sum2,m);
                if(sum1 == sum2 && sum1 == m)
                {
                    printf("%d %d\n",i,j);
                    flag = 1;
                    break;
                }
            }
            if(flag == 1)
            {
                break;
            }
        }
    }
    return 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
时间: 2024-07-29 22:54:36

UVA Birthday Cake (一条直线平分草莓)的相关文章

Interview Q&amp;A - 在一个圆圈里有有限个点(数量一定为偶数),是否一定可以找到一条直线不穿过任意一个点并且把所有的点平分成两半?

从圆圈外考虑这个问题. 随意找一条与圆相离的直线L,然后过每两个点作一条直线,标记与L的交点(若平行则忽略),然后以L上任意一个未标记的点为中心开始旋转L,则必定旋转到某个角度时,直线L恰好平分所有点.

UVA Lining Up (一条直线上最多的点数)

Description  Lining Up  ``How am I ever going to solve this problem?" said the pilot. Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over

【画一条直线】我们第一个(极小的)机器学习应用

画一条怎样的线? 在回答这个问题之前我们必须要了解另外两个问题: 1.为什么要画这条直线? 2.这条直线要具备怎样的特征? 其实这两个问题并不好回答,因为这必然会牵扯到机器学习的理论方面,我真的不想扯那么远(其实我不是很懂怕说错了),只从我们现在要处理的这个实际问题问题——“网站每小时的访问量”入手. 我们要解决的实际问题是:根据现有的访问量变化趋势,来预测未来访问量的变化,看是否会超出现有设备的承受范围,给出时间点,以便网站方能够做好应对方案. 可以说——总结规律,预测未来——就是我们要做的,

一条直线上N个线段所覆盖的总长度

原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线段总共覆盖了多长?(重复覆盖的地区只计算一次) ================================================ 解题思路: 可以将每个线段拆分成“单位1” 遍历所有线段,使用一个数组记录每个线段所走过的“单位1” 最后统计数组中被走过的中“单位1”的个数,即是所有线

求空间内两条直线的最近距离以及最近点的坐标(C++)

关键词:空间几何 用途:总有地方会用到吧 文章类型:C++函数展示 @Author:V_Shawn @Date:2016-11-19 @Lab: [email protected] 对于空间内的两条直线A,B我假设他们一定不平行,也不相交.若已知A上两点A1.A2,B上两点B1.B2,那么可以很容易得到两条直线的方程. 然后调用函数即可解得结果: 下面提供了一个用于解决这个问题的类: #include <math.h> //用于求解两条空间直线的最近距离,以及他们最近的两点坐标 //autho

求两条直线(线段)的交点

转自: http://blog.csdn.net/dgq8211/article/details/7952825 如图,如何求得直线 AB 与直线 CD 的交点P? 以上内容摘自<算法艺术与信息学竞赛>. 思路就是利用叉积求得点P分线段DC的比,然后利用高中学习的定比分点坐标公式求得分点P的坐标. 看不懂的可以去复习下 定比分点 的知识. 1 #include <math.h> 2 #include <stdio.h> 3 #include <string.h&g

计算两条直线的交点(C#)

从其他地方看到的源码是有问题的. /// <summary> /// 计算两条直线的交点 /// </summary> /// <param name="lineFirstStar">L1的点1坐标</param> /// <param name="lineFirstEnd">L1的点2坐标</param> /// <param name="lineSecondStar"

判断两条直线是否相交点

#pragma mark ------------ 判断两条直线是否相交 + (BOOL)checkLineIntersection:(CGPoint)p1 p2:(CGPoint)p2 p3:(CGPoint)p3 p4:(CGPoint)p4 {     CGFloat denominator = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y);          if (denominator == 0.0f) {

poj1039——计算几何 求直线与线段交点,判断两条直线是否相交

poj1039——计算几何  求直线与线段交点,判断两条直线是否相交 Pipe Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9439   Accepted: 2854 Description The GX Light Pipeline Company started to prepare bent pipes for the new transgalactic light pipeline. During the de