A - Two Rectangles AtCoder-2284

          A - Two Rectangles

There are two rectangles. The lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B. The lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D.

Print the area of the rectangle with the larger area. If the two rectangles have equal areas, print that area.

Constraints

  • All input values are integers.
  • 1A104
  • 1B104
  • 1C104
  • 1D104
Input

The input is given from Standard Input in the following format:

A B C D
Output

Print the area of the rectangle with the larger area. If the two rectangles have equal areas, print that area.

Sample Input 1

3 5 2 7
Sample Output 1

15

The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14. Thus, the output should be 15, the larger area.

Sample Input 2

100 600 200 300
Sample Output 2

60000


AC代码:
                  Select Code

#include<stdio.h>
int main()
{
	int a,b,c,d,s1,s2;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	s1=a*b;
	s2=c*d;
	if(s1>=s2)
	  printf("%d\n",s1);
	else
	  printf("%d\n",s2);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你想让我设计一个用于计算矩形集合并的分治算法。输入的矩形都是与坐标轴对齐的,并且它们都与 y 轴相交。每个矩形由其左上角和右下角的坐标表示,集合并由从上到下列出的内部不相交的矩形序列表示。我们要求序列中的任意两个相邻矩形都不能合并成一个矩形。 解题思路:我们可以使用类似于归并排序的方法来实现这个算法。具体来说,我们可以将输入的矩形集合分成两个部分,然后分别计算它们的联合。最后,我们可以将这两个联合合并成一个联合。 我们可以通过以下步骤来实现这个算法: 1. 如果矩形集合为空,则返回一个空的序列。 2. 如果矩形集合只包含一个矩形,则返回该矩形。 3. 将矩形集合分成两个部分,分别计算它们的联合。 4. 合并这两个联合,生成一个新的联合。 5. 对于新联合中的相邻矩形,如果它们可以合并,则将它们合并成一个矩形。 6. 返回新联合。 代码实现如下: ```python class Rectangle: def __init__(self, x1, y1, x2, y2): self.x1, self.y1, self.x2, self.y2 = x1, y1, x2, y2 def merge_rectangles(rectangles): def merge(left, right): if not left: return right if not right: return left i, j = 0, 0 res = [] while i < len(left) and j < len(right): if left[i].y2 <= right[j].y1: res.append(left[i]) i += 1 elif left[i].y1 >= right[j].y2: res.append(right[j]) j += 1 else: if left[i].x2 == right[j].x1: res.append(Rectangle(left[i].x1, left[i].y1, right[j].x2, right[j].y2)) i += 1 j += 1 elif left[i].x1 == right[j].x2: res.append(Rectangle(right[j].x1, right[j].y1, left[i].x2, left[i].y2)) i += 1 j += 1 elif left[i].x2 <= right[j].x1: res.append(left[i]) i += 1 elif left[i].x1 >= right[j].x2: res.append(right[j]) j += 1 else: if left[i].x1 < right[j].x1: res.append(Rectangle(left[i].x1, left[i].y1, right[j].x1, left[i].y2)) left[i] = Rectangle(right[j].x1, left[i].y1, left[i].x2, left[i].y2) elif left[i].x1 > right[j].x1: res.append(Rectangle(right[j].x1, right[j].y1, left[i].x1, right[j].y2)) right[j] = Rectangle(left[i].x1, right[j].y1, right[j].x2, right[j].y2) elif left[i].x2 > right[j].x2: res.append(Rectangle(left[i].x1, left[i].y1, right[j].x2, left[i].y2)) left[i] = Rectangle(right[j].x2, left[i].y1, left[i].x2, left[i].y2) elif left[i].x2 < right[j].x2: res.append(Rectangle(left[i].x1, left[i].y1, left[i].x2, left[i].y2)) right[j] = Rectangle(left[i].x2, right[j].y1, right[j].x2, right[j].y2) res.extend(left[i:]) res.extend(right[j:]) return res if len(rectangles) == 0: return [] if len(rectangles) == 1: return rectangles mid = len(rectangles) // 2 left, right = rectangles[:mid], rectangles[mid:] left = merge_rectangles(left) right = merge_rectangles(right) res = merge(left, right) i = 0 while i < len(res) - 1: if res[i].x1 == res[i+1].x1 and res[i].x2 == res[i+1].x2: res[i] = Rectangle(res[i].x1, res[i].y1, res[i].x2, res[i+1].y2) del res[i+1] else: i += 1 return res ``` 这个算法的时间复杂度是 O(nlogn),其中 n 是矩形的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值