题目描述
Bamboo Leaf Rhapsody (竹叶狂想曲)
Do you know who will fulfill people’s wishes on Tanabata?
It’s α Lyra and α Aquila.
By the way, Earth is 25 light-years away from α Lyra and 16 light-years away from α Aquila. That is to say, it will take 25 or 16 years for the messages sent from Earth to reach these two constellations. That’s for granted. Do you understand?
– The Boredom of Haruhi Suzumiya, 2004
This is the 16th year since the publication of the novel. Apart from the short stories, the author has not updated the series for nine years.
Setsuna is a big fan of the novel. She makes a wish that the author would update as soon as possible, and she will send this message to the stars.
The universe can be seen as a three-dimensional rectangular coordinate system in which the earth is the origin(i.e., the coordinate of the earth is (0,0,0)).
Suppose there are n stars, and the coordinate of the i-th one is (xi,yi,zi). Any one of them can receive her message.
The speed of the message is exactly 1 unit per year in the universe, and the direction can be arbitrary.
关键信息部分翻译:
以宇宙可以看作是一个以地球为原点的三维直角坐标系。地球坐标为(0,0,0))。
假设有n颗星,第i个星的坐标是(xi,yi,zi)他们中的任何人都能收到她的信息。
信息的速度在宇宙中正好是一年一个单位,方向可以是任意的
Setsuna wants to know the shortest time for the message to reach at least one of the stars after she sends it.
You can assume that Einstein’s theory of relativity doesn’t work in Setsuna’s world.
输入描述
The first line contains one integer n(1≤n≤1000), indicating the number of the stars.
The i-th of the next n lines contains three integers xi,yi,zi(−1000≤xi,yi,zi≤1000), indicating the coordinate of the i-th star.
输出描述
Output the shortest time in years, the value should be rounded to three decimal places.
输入输出样例
Input
3
0 1 1
2 0 0
0 -2 0
Output
1.414
线索提示
The first star can receive the message in 2 \sqrt{2} 2 years.
The second and the third star can receive the message in 2 years.
So the answer is 2 \sqrt{2} 2 years, and it should be rounded to 1.414 .
一、解题思路
上面说了一大堆,翻译过来的人话就是:
第一个输入的数字n代表案例(星球)的个数
以宇宙可以看作是一个以地球为原点的三维直角坐标系。地球坐标为(0,0,0)),现在要你求哪个星球距离地球距离最短。
- 在每个案例中输入x,y,z三个数字,表示距离地球的坐标
- 可以用我们学过的三维坐标求距离公式得到每个星球和地球的距离,并将所得的值存在数组里面
- 用 < a l g o r i t h m > <algorithm> <algorithm>头文件中sort()排序函数对数组进行排序
- 最后用printf进行格式化输出
d i = ( x i − x ) 2 + ( y i − y ) 2 + ( z i − z ) 2 d_i=\,\,\sqrt{\left( x_i-x \right) ^2\,\,+\,\,\left( y_i-y \right) ^2+\left( z_i-z \right) ^2} di=(xi−x)2+(yi−y)2+(zi−z)2
二、题解
源代码
代码如下:
/*
* Author: FeverTwice
* Date: 2021-05-02
* Func: Solution for Bamboo Leaf Rhapsody
*/
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <math.h>
#include <functional>
#define debug(a) cout<<#a<<"="<<a<<endl;
#define lyh(i,a,b) for(int i=a;i<=b;i++)
#define hyl(i,a,b) for(int i=a;i>=b;i--)
#define LL long long
#define mm memset
#define EPS 1e-8
#define INF 0x7fffffff
using namespace std;
int main()
{
ios::sync_with_stdio(0); //加快cin的输入流读写处理速度
int x, y, z;
int n;
cin >> n;
int *arr = new int[n]; //创建动态数组
for (int i = 0; i < n; i++)
{
cin >> x >> y >> z;
arr[i] = pow(x, 2) + pow(y, 2) + pow(z, 2);
}
sort(arr, arr + n);
printf("%.3lf", sqrt(1.0 * arr[0]));
return 0;
}
VJudge评判结果
写在最后
各位看官,都看到这里了,麻烦动动手指头给博主来个点赞8,您的支持作者最大的创作动力哟! <(^-^)>
才疏学浅,若有纰漏,恳请斧正
本文章仅用于各位同志作为学习交流之用,不作任何商业用途,若涉及版权问题请速与作者联系,望悉知