您的位置:首页 > 其它

UVA - 152 - Tree's a Crowd

2014-04-27 21:48 225 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=88

题意:

给出N个三维数,对于每个点,找出与其它点最小的距离,如距离小于1,则1,0,0,0,0,0,0,0,0,0,0←0,0,0,0,0,0,0,0,0,0。

每个点都得检索出来,并统计起来。

解题:

如题意,直接保存后开始计算距离。每个点都只找最小距离(与其它点的距离),再统计。(如,对于A点,有到点B距离2.5,有到点C距离3.5,则2.5有效)

写了个类,复习一下重载~虽然重载了,可发觉用不上。。。当复习好了。

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <string.h>
#include <stdio.h>
using namespace std;

// #define LOCAL_TEST

class CPoint
{
public:
int x;
int y;
int z;
CPoint (int x = 0, int y = 0, int z = 0)
{
this->x = x;
this->y = y;
this->z = z;
}
double fDistance(CPoint p)
{
return sqrt( (double) ((this->x-p.x)*(this->x-p.x)
+ (this->y-p.y)*(this->y-p.y) + (this->z-p.z)*(this->z-p.z) ) );
}
double operator - (CPoint p)
{
return fDistance(p);
}
friend istream &operator << (istream& input, CPoint& p);
};

istream &operator << (istream& input, CPoint& p)
{
input >>p.x >>p.y >>p.z;
return input;
}

int main()
{
#ifdef LOCAL_TEST
freopen("f:\\in.txt", "r", stdin);
freopen("f:\\out.txt", "w+", stdout);
#endif
CPoint p;
int a, b, c;
vector <CPoint> vPt;
vector <double> vDis;
int szCount[12];
memset(szCount, 0, sizeof(szCount));

// get all input data
while ( cin >>a >> b >>c )
{
if ( a == 0 && b == 0 && c == 0 )
break;
CPoint pTmp(a, b, c);
vPt.push_back(pTmp);
} // end while

// calc all distance while index'i!=k
for ( int i=0; i<vPt.size(); i++ )
{
double dis = 11;
// get nearest distance
for ( int k=0; k<vPt.size(); k++ )
if ( i != k && vPt[i] - vPt[k] < dis )
dis = vPt[i] - vPt[k];
if ( dis < 10 + 1e-7 )
{	// count corresponding
int index = (int)(dis) + 1;
szCount[index]++;
} // end if
} // end for

// format output
for ( int i=1; i<=10; i++ )
cout <<setiosflags(ios::right) <<setw(4) <<szCount[i];
cout <<'\n';

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: