UVA - 152 - Tree's a Crowd

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值