读取若干个点,每个点放入元组。并将所有点的点信息、点的类型、点与原点的距离打印出来。
函数接口定义:
readPoint() #从一行以,分隔的数中读取坐标,放入元组并返回
distance(point) #计算point与原点的距离并返回,要math库中的函数
裁判测试程序样例:
/* 请在这里填写答案 */
n = int(input())
for i in range(n):
p = readPoint()
print('Point = {}, type = {}, distance = {:.3f}'.format(p,type(p),distance(p)))
输入格式:
输入n,代表底下要输入n行点坐标。坐标全部为整数。
点坐标x,y,z以,
分隔。坐标全部为整数。
注意:坐标以,
分隔,相应位置可能无字符或者包含多个空格字符,读入时按照0进行处理。
输出格式:
见输出样例
输入样例:
5
1,1,1
,,
2,,1
3,1,3
5,,
结尾无空行
输出样例:
Point = (1, 1, 1), type = <class 'tuple'>, distance = 1.732
Point = (0, 0, 0), type = <class 'tuple'>, distance = 0.000
Point = (2, 0, 1), type = <class 'tuple'>, distance = 2.236
Point = (3, 1, 3), type = <class 'tuple'>, distance = 4.359
Point = (5, 0, 0), type = <class 'tuple'>, distance = 5.000
结尾无空行
解析:
这个题目不难,主要是对于输入来说,需要思考一下,第一输入进去的元组可能会省略0,所以我们需要加上,所以加一个判断即可:
if i != '': #如果取出来的字符串不是空字符串,就把数字继承,如果是空字符串,那就是省略了,继承0
m.append(int(i))
else:
m.append(0)
答案:
def readPoint(): # 从一行以,分隔的数中读取坐标,放入元组并返回
n = input()
n = n.split(',')
m = []
for i in n:
if i != '':
m.append(int(i))
else:
m.append(0)
m = tuple(m)
return m
def distance(point): # 计算point与原点的距离并返回,要math库中的函数
sum1 = 0
for i in point:
sum1 += i ** 2
return sum1 ** 0.5