uva-152

蛮水的题目,开始觉得好像直接比较大小不能过一样,结果证明是我想多了。

题意:将空间中的点按照到邻近点的最小距离分区间[0,1)~[9,10)

代码:

//#define LOCAL
#include <stdio.h>
#include <string.h>
#include <math.h>

#define MAXN 5000 + 10 // 最大树的数目 

int calcDistance(int i, int j);// 参考了别人的想法,将计算的结果直接强转成int 

struct TreePoint // 保存每个点的位置 
{
	int x;
	int y;
	int z;
}trees[MAXN]; 

int result[11]; // 保存最后的结果 

int main()
{
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	int a, b, c;
	int count, i, j;// 标记变量 
	// 数据初始化	
	memset(trees, 0, sizeof(trees));
	memset(result, 0, sizeof(result));
	// 算法输入 
	count = 0;	
	while(scanf("%d%d%d", &a, &b, &c) == 3 && a + b + c != 0)
	{
		trees[count].x = a; 
		trees[count].y = b; 
	 	trees[count].z = c; 
	 	count++; 
	}
 	
	for(i = count - 1; i >= 0; i--) 
	{
		int mindistance = 500;
		for(j = count - 1; j >= 0; j--) 
		{
			if(i != j)
			{ 
				int distance = calcDistance(i, j);
				// printf("distance=%d\n", distance);
				if(distance < mindistance)
				{
					mindistance = distance;
				}
			} 
		}
	 	//printf("mindistance=%d\n", mindistance);
		if(mindistance < 10)
			result[mindistance]++;
	}
	
	for(i = 0; i < 10; i++)
	{
		printf("%4d", result[i]);
	}
	printf("\n");
	return 0;
}


int calcDistance(int i, int j)// 参考了别人的想法,将计算的结果直接强转成int 
{
	int x, y, z;
	x = trees[i].x - trees[j].x;
	// printf("x=%d\n", x);
	y = trees[i].y - trees[j].y;
	// printf("y=%d\n", y);
	z = trees[i].z - trees[j].z;
	// printf("z=%d\n", z);
	return  floor(sqrt((x * x + y * y + z * z)));
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值