uva152

来源:互联网 发布:淘宝买家申请售后换货 编辑:程序博客网 时间:2024/06/07 16:00

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=88

152 - Tree's a Crowd

Time limit: 3.000 seconds

Tree's a Crowd

Dr William Larch, noted plant psychologist and inventor of thephrase ``Think like a tree--Think Fig'' has invented a newclassification system for trees. This is a complicated systeminvolving a series of measurements which are then combined toproduce three numbers (in the range [0, 255]) for any given tree.Thus each tree can be thought of as occupying a point in a3-dimensional space. Because of the nature of the process,measurements for a large sample of trees are likely to be spreadfairly uniformly throughout the whole of the available space.However Dr Larch is convinced that there are relationships to befound between close neighbours in this space. To test thishypothesis, he needs a histogram of the numbers of trees that haveclosest neighbours that lie within certain distance ranges.

Write a program that will read in the parameters of up to 5000trees and determine how many of them have closest neighbours thatare less than 1 unit away, how many with closest neighbours 1 ormore but less than 2 units away, and so on up to those with closestneighbours 9 or more but less than 10 units away. Thus if tex2html_wrap_inline26 is the distance between the i'th point and itsnearest neighbour(s) and tex2html_wrap_inline28 , with j and k integers andk = j+1, then this point (tree) will contribute 1 tothe j'th bin in the histogram (counting from zero). For example, ifthere were only two points 1.414 units apart, then the histogramwould be 0, 2, 0, 0, 0, 0, 0, 0, 0, 0.

Input andOutput

Input will consist of a series of lines, each line consisting of3 numbers in the range [0, 255]. The file will be terminated by aline consisting of three zeroes.

Output will consist of a single line containing the 10 numbersrepresenting the desired counts, each number right justified in afield of width 4.

Sampleinput

10 10 010 10 010 10 110 10 310 10 610 10 1010 10 1510 10 2110 10 2810 10 3610 10 450 0 0

Sampleoutput

   2   1   1   1   1   1   1   1   1   1
题意:其一棵树和它相邻最近(范围>=0&&<10)的树的个数。。
思路:直接暴力,枚举。。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define N 5010
struct Point{
 int x;
 int y;
 int z;
}point[N];//定义x,y,z的坐标
int main()
{
 memset(point,0,sizeof(point));
 int i=0,j=0,n=0,count[20];
 memset(count,0,sizeof(count));
 while(1)//三个数同时为零跳出循环
 {
  scanf("%d %d %d",&point[i].x,&point[i].y,&point[i].z);
  if(point[i].x ==0&&point[i].y ==0&&point[i].z==0)
   break;
  i++;n++;
 }
 for(i=0;i<n;i++)
 {
  int k=0,min=10;//只要求范围在>=0<10之内的数据,其他的无效,min的位置必须在这里
  for(j=0;j<n;j++)
  {
   if(i==j)//自己不能和自己比
    continue;
   k=(int)pow((float)point[i].x -point[j].x ,2)+pow((float)(point[i].y -point[j].y),2)+pow((float)(point[i].z -point[j].z ),2);//求距离
   k=(int )sqrt(k);//开方
   if(k<min)min=k;
  }
  count[min]++;//所有最小距离的累加
 }
 for(i=0;i<10;i++)
  printf("M",count[i]);
 printf("\n");
 return 0;
}
原创粉丝点击