当前位置: 首页 > news >正文

openJudge | 距离排序

总时间限制: 1000ms 内存限制: 65536kB

描述

给出三维空间中的n个点(不超过10个),求出n个点两两之间的距离,并按距离由大到小依次输出两个点的坐标及它们之间的距离。

输入

输入包括两行,第一行包含一个整数n表示点的个数,第二行包含每个点的坐标(坐标都是整数)。点的坐标的范围是0到100,输入数据中不存在坐标相同的点。

输出

对于大小为n的输入数据,输出n*(n-1)/2行格式如下的距离信息:
(x1,y1,z1)-(x2,y2,z2)=距离
其中距离保留到数点后面2位。
(用cout输出时保留到小数点后2位的方法:cout<

样例输入

4
0 0 0 1 0 0 1 1 0 1 1 1

样例输出

(0,0,0)-(1,1,1)=1.73
(0,0,0)-(1,1,0)=1.41
(1,0,0)-(1,1,1)=1.41
(0,0,0)-(1,0,0)=1.00
(1,0,0)-(1,1,0)=1.00
(1,1,0)-(1,1,1)=1.00

提示

用cout输出时保留到小数点后2位的方法:cout<<fixed<<setprecision(2)<<x

注意:

冒泡排序满足下面的性质,选择排序和快速排序(qsort或sort)需要对下面的情况进行额外处理
使用冒泡排序时要注意边界情况的处理,保证比较的两个数都在数组范围内

  1. 对于一行输出中的两个点(x1,y1,z1)和(x2,y2,z2),点(x1,y1,z1)在输入数据中应出现在点(x2,y2,z2)的前面。

比如输入:

2
0 0 0 1 1 1

输出是:

(0,0,0)-(1,1,1)=1.73

但是如果输入:

2
1 1 1 0 0 0

输出应该是:

(1,1,1)-(0,0,0)=1.73
  1. 如果有两对点p1,p2和p3,p4的距离相同,则先输出在输入数据中靠前的点对。

比如输入:

3
0 0 0 0 0 1 0 0 2

输出是:

(0,0,0)-(0,0,2)=2.00
(0,0,0)-(0,0,1)=1.00
(0,0,1)-(0,0,2)=1.00

如果输入变成:

3
0 0 2 0 0 1 0 0 0

则输出应该是:

(0,0,2)-(0,0,0)=2.00
(0,0,2)-(0,0,1)=1.00
(0,0,1)-(0,0,0)=1.00

答案

#include <stdio.h>
#include <math.h>
typedef struct {int start[3];int end[3];double dis;int weight;
} points;
int main() {static int n, p=0;static int a[10][4];static points point[1024], t;scanf("%d", &n);for(int i = 0; i < n; i++) {scanf("%d %d %d", &a[i][0], &a[i][1], &a[i][2]);}for(int i = 0; i < n; i++) {for(int j = i+1; j < n; j++) {point[p].start[0] = a[i][0];point[p].start[1] = a[i][1];point[p].start[2] = a[i][2];point[p].end[0] = a[j][0];point[p].end[1] = a[j][1];point[p].end[2] = a[j][2];point[p].dis = sqrt((a[i][0]-a[j][0])*(a[i][0]-a[j][0])+(a[i][1]-a[j][1])*(a[i][1]-a[j][1])+(a[i][2]-a[j][2])*(a[i][2]-a[j][2]));point[p].weight = p;p++;}}for(int i = 0; i < p; i++) {for(int j = i+1; j < p; j++) {if(point[i].dis < point[j].dis) {t = point[i];point[i] = point[j];point[j] = t;} else if(point[i].dis == point[j].dis) {if(point[i].weight > point[j].weight) {t = point[i];point[i] = point[j];point[j] = t;}}}}for(int i = 0; i < p; i++) {printf("(%d,%d,%d)-(%d,%d,%d)=%.2f\n", point[i].start[0], point[i].start[1], point[i].start[2], point[i].end[0], point[i].end[1], point[i].end[2], point[i].dis);}
}

至于weight,它的作用,就只是增加一个权重罢了。

http://www.lryc.cn/news/298513.html

相关文章:

  • 【算法】排序详解(快速排序,堆排序,归并排序,插入排序,希尔排序,选择排序,冒泡排序)
  • LeetCode Python -8.字符串转整数
  • 【java】笔记10:类与对象——本章练习
  • 《UE5_C++多人TPS完整教程》学习笔记8 ——《P9 访问 Steam(Acessing Steam)》
  • 缓存穿透问题与解决方案
  • 《Git 简易速速上手小册》第1章:Git 基础(2024 最新版)
  • 交易中的胜率和盈亏比估算
  • mysql RR、RC隔离级别实现原理
  • c语言--指针数组(详解)
  • Elasticsearch单个索引数据量过大的优化
  • Java安全 CC链1分析(Lazymap类)
  • 【lesson51】信号之信号处理
  • 分享springboot框架的一个开源的本地开发部署教程(若依开源项目开发部署过程分享持续更新二开宝藏项目MySQL数据库版)
  • leetcode:131.分割回文串
  • Linux下的json-c
  • [C#] 如何使用ScottPlot.WPF在WPF桌面程序中绘制图表
  • 如何修复Mac的“ kernel_task” CPU使用率过高的Bug?
  • 【NodeJS】006- API模块与会话控制介绍d
  • [UI5 常用控件] 08.Wizard,NavContainer
  • EasyExcel分页上传数据
  • Spring Native 解放 JVM
  • 汇编的两道题
  • Seurat - 聚类教程 (1)
  • Mac 版 Excel 和 Windows 版 Excel的区别
  • 【报错解决】-bash: export: `-8‘: not a valid identifier 不是有效的标识符
  • Docker-Learn(三)创建镜像Docker(换源)
  • 「递归算法」:二叉树剪枝
  • Kafka下载(kafka和jdk、zookeeper、SpringBoot的版本对应关系)
  • 自然语言NLP
  • 容器库(5)-std::list