【发布时间】:2016-04-24 15:18:59
【问题描述】:
我有一个矩形的三个顶点,需要找到第四个顶点,我需要为 N 个矩形找到缺失的顶点。
遗憾的是,我不知道如何在第一个矩形之后分配顶点:/。
这是输入的示例文本文件:
2 # '2' is the number of rectangles.
5 5 # (x1, y1)
5 7 # (x2, y2)
7 5 # (x3, y3)
30 20 # (x1, y1)
10 10 # (x2, y2)
10 20 # (x3, y3)
# (there could be more '**vertices**' and more than '**2**' cases)
这是我的方法:
import sys
def calculate(num):
x1 = lines[n].split()[0]
y1 = lines[n].split()[1]
x2 = lines[n+1].split()[0]
y2 = lines[n+1].split()[1]
x3 = lines[n+2].split()[0]
y3 = lines[n+2].split()[1]
print x1, y1
print x2, y2
print x3, y3
#Planning to write codes for calculation & results below inside this function.
readlines = sys.stdin.readlines() # reads
num = int(lines[0]) # assigns the number of cases
for i in range(0, num):
item += 1
calculate(item) # Calls the above function
当我运行这段代码时,我得到以下信息:
5 5
5 7
7 5
5 7
7 5
30 20
我想得到的是:
5 5
5 7
7 5
30 20
10 10
10 20
【问题讨论】:
-
使用
itertools文档页面上显示的grouper配方,您可以轻松(有效地)以三个为一组来使用您的文件。 -
这是我第一次在这里注册并发布我的问题。非常感谢您的帮助!
-
什么是
'n?你在哪里申报的?你上面贴的代码好像不完整 -
我的错。是的,你是对的。它应该是'n'而不是'num'! :)
标签: python loops for-loop assign