亲宝软件园·资讯

展开

Python计算两个矩形重合面积代码实例

人气:0

这篇文章主要介绍了Python 实现两个矩形重合面积代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

计算两个矩形的重合面积

import math
x1, y1, x2, y2 = input().split(" ")
x1, y1, x2, y2=int(x1), int(y1), int(x2), int(y2)
# print(x1, y1, x2, y2)
x1,x2 = min(x1,x2),max(x1,x2)
y1,y2= min(y1,y2),max(y1,y2)

# print(x1, y1, x2, y2)

x3, y3, x4, y4 = input().split(" ")
x3, y3, x4, y4 = int(x3), int(y3), int(x4), int(y4)
x3,x4 = min(x3,x4),max(x3,x4)
y3,y4 = min(y3,y4),max(y3,y4)

# print(x3, y3, x4, y4)

if (x2<=x3 or x4<=x1) and (y2 <= y3 or y4<=y1):
  print(0)
else:
  lens = min(x2, x4) - max(x1, x3)
  wide = min(y2, y4) - max(y1, y3)
  print(lens*wide)

字符串重叠输出

import string
in_str = input()
nums = string.digits

a = ""
strs = ""
num = ""
for i in range(len(in_str)):
  if in_str[i] not in nums:
    a = a+in_str[i]
  else:
    if i == len(in_str)-1 or (in_str[i+1] not in nums):
      num = int(num + in_str[i])
      strs = strs + a*num
      a=""
      num=""
    else:
      num = num + in_str[i]
print(strs)
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论