设为首页 加入收藏

TOP

10167 - Birthday Cake
2012-11-04 13:52:41 来源: 作者: 【 】 浏览:300
Tags: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 (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
    以前这个题目还不会做,现在真正静下心来看还是很容易的
    [cpp]
    #include <iostream>
    #include <cmath>
    using namespace std;
    int a[100] ;
    int main()
    {
    double A,B,s;
    int i,j,n,m,t,s1;
    while(cin》n)
    {
    if(n==0)
    {
    break;
    }
    for(i=0;i<=2*n-1;i++)
    {
    cin》a[i][0]》a[i] ;
    }
    for(A=-500;A<=500;A++)
    {
    for(B=-500;B<=500;B++)
    {
    if(A!=0&&B!=0)
    {
    s1=0;
    for(i=0;i<=2*n-1;i++)
    {
    s=A*a[i][0]+B*(a[i] );
    if(fabs(s)<=1e-7)
    {
    break;
    }else if(s>0)
    {
    s1++;
    }
    }
    if(i==2*n&&s1==n)
    {
    break;
    }
    }else if(A==0&&B!=0)
    {
    s1=0;
    for(i=0;i<=2*n-1;i++)
    {
    if(fabs(a[i] )<=1e-7)
    {
    break;
    }else if(a[i] >0)
    {
    s1++;
    }
    }
    if(i==2*n&&s1==n)
    {
    break;
    }
    }else if(A!=0&&B==0)
    {
    s1=0;
    for(i=0;i<=2*n-1;i++)
    {
    if(fabs(a[i][0])<=1e-7)
    {
    break;
    }else if(a[i][0]>0)
    {
    s1++;
    }
    }
    if(s1==n&&i==2*n)
    {
    break;
    }
    }
    }
    if(B!=501)
    {
    cout《A《" "《B《endl;
    break;
    }
    }
    }
    return 0;
    }

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇如何输出一个螺旋矩阵 下一篇搜狐2012校园招聘会笔试题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: