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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章