HDU 1173 采矿

某天gameboy玩魔兽RPG。有一个任务是在一个富含金矿的圆形小岛上建一个基地,以最快的速度采集完这个小岛上的所有金矿。这个小岛上有n(0<n<1000000)个金矿,每个金矿的矿藏量是相等的。而且这个小岛的地势非常平坦,所以基地可以建在小岛的任何位置,每个金矿的采矿速度只跟矿藏到基地的路程长度有关。为了不让这个任务太无聊,游戏设计者对这个小岛施了个“魔法”,规定矿工在小岛上只能正南正北正西正东走。也就是说矿工不能斜着在岛上走。

这个小岛在一个二维直角坐标系中描述。

你的任务就是帮gameboy找一个建造基地的位置,使矿工能以最快的速度采完所有矿。
 

Input
输入数据有多组。每组数据的第一行是一个正整数n(0<n<1000000),表示小岛上有n个金矿。在接下来的n行中,每行有两个实数x,y,表示其中一个金矿的坐标。n=0表示输入数据结束。
 

Output
每一组输入数据对应一行输出,输出两个实数x,y(保留小数点后两位),也就是你找到的建造基地的位置坐标。如果坐标不唯一,可以任选一个输出。
 

Sample Input
4 1.0 1.0 3.0 1.0 3.0 3.0 1.0 3.0 0
 

Sample Output
2.00 2.00
 
刚看到题目时都不知道怎么做,没想到这么水,中位数????
 
C

#include <stdio.h>
#include <stdlib.h>
double x[1000000],y[1000000];
int cmp(const void *a,const void *b)
{
    return (*(double *)a)>(*(double *)b)?1:-1;
}
int main()
{
    freopen("in.txt","r",stdin);
    int n,i;
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)
          scanf("%lf%lf",&x[i],&y[i]);
        qsort(x,n,sizeof(x[0]),cmp);
        qsort(y,n,sizeof(y[0]),cmp);
        printf("%.2lf %.2lf\n",x[n/2],y[n/2]);
    }
    return 0;
}

C++
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
double x[1000000],y[1000000];
bool cmp(double a,double b)
{
    return a<b;
}
int main()
{
    freopen("in.txt","r",stdin);
    int n,i;
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)
          scanf("%lf%lf",&x[i],&y[i]);
        sort(x,x+n,cmp);
        sort(y,y+n,cmp);
        printf("%.2lf %.2lf\n",x[n/2],y[n/2]);
    }
 return 0;
}

 

posted on 2012-04-06 14:43  江财小子  阅读(293)  评论(0编辑  收藏  举报