蓝桥杯试题,矩形面积交,写一个c语言代码
时间: 2023-09-06 21:05:10 浏览: 101
### 回答1:
好的,这是一份 C 语言代码,用于计算两个矩形的面积交。
```
#include <stdio.h>
struct Rectangle {
int x1, y1, x2, y2;
};
int max(int a, int b) {
return a > b ? a : b;
}
int min(int a, int b) {
return a < b ? a : b;
}
int overlap(struct Rectangle a, struct Rectangle b) {
int x_overlap = max(0, min(a.x2, b.x2) - max(a.x1, b.x1));
int y_overlap = max(0, min(a.y2, b.y2) - max(a.y1, b.y1));
return x_overlap * y_overlap;
}
int main() {
struct Rectangle a, b;
printf("请输入第一个矩形的坐标(x1 y1 x2 y2):");
scanf("%d%d%d%d", &a.x1, &a.y1, &a.x2, &a.y2);
printf("请输入第二个矩形的坐标(x1 y1 x2 y2):");
scanf("%d%d%d%d", &b.x1, &b.y1, &b.x2, &b.y2);
printf("两个矩形的面积交为:%d\n", overlap(a, b));
return 0;
}
```
### 回答2:
下面是一个用C语言实现计算矩形面积交的代码:
```c
#include <stdio.h>
int main() {
// 输入两个矩形的左上角和右下角坐标
int x1, y1, x2, y2;
int x3, y3, x4, y4;
printf("请输入第一个矩形的左上角坐标(x1, y1): ");
scanf("%d%d", &x1, &y1);
printf("请输入第一个矩形的右下角坐标(x2, y2): ");
scanf("%d%d", &x2, &y2);
printf("请输入第二个矩形的左上角坐标(x3, y3): ");
scanf("%d%d", &x3, &y3);
printf("请输入第二个矩形的右下角坐标(x4, y4): ");
scanf("%d%d", &x4, &y4);
// 计算矩形的宽度和高度
int width = 0, height = 0;
if (x1 <= x4) {
if (x2 <= x3) {
printf("矩形面积交为0\n");
return 0;
}
else {
width = x2 - x3;
}
}
else {
if (x4 <= x1) {
printf("矩形面积交为0\n");
return 0;
}
else {
width = x4 - x1;
}
}
if (y1 <= y4) {
if (y2 <= y3) {
printf("矩形面积交为0\n");
return 0;
}
else {
height = y2 - y3;
}
}
else {
if (y4 <= y1) {
printf("矩形面积交为0\n");
return 0;
}
else {
height = y4 - y1;
}
}
// 计算矩形面积交
int area = width * height;
printf("矩形面积交为:%d\n", area);
return 0;
}
```
运行程序后,会提示输入两个矩形的左上角和右下角坐标。根据输入的坐标,程序会计算矩形的宽度和高度,并判断是否有面积交。如果有面积交,程序会计算并输出矩形面积交;如果没有面积交,则输出0。
### 回答3:
下面是一个使用C语言实现矩形面积交的代码示例:
```c
#include<stdio.h>
// 定义矩形结构体
typedef struct {
int x; // 左下角的x坐标
int y; // 左下角的y坐标
int width; // 矩形的宽度
int height; // 矩形的高度
} Rectangle;
// 计算矩形面积交的函数
int getIntersectionArea(Rectangle rect1, Rectangle rect2) {
// 判断两个矩形是否有重叠部分
if (rect1.x + rect1.width <= rect2.x || rect2.x + rect2.width <= rect1.x ||
rect1.y + rect1.height <= rect2.y || rect2.y + rect2.height <= rect1.y) {
return 0; // 没有重叠部分,面积交为0
}
// 计算重叠部分的左下角坐标
int x = rect1.x > rect2.x ? rect1.x : rect2.x;
int y = rect1.y > rect2.y ? rect1.y : rect2.y;
// 计算重叠部分的右上角坐标
int right = (rect1.x + rect1.width) < (rect2.x + rect2.width) ? (rect1.x + rect1.width) : (rect2.x + rect2.width);
int top = (rect1.y + rect1.height) < (rect2.y + rect2.height) ? (rect1.y + rect1.height) : (rect2.y + rect2.height);
// 计算重叠部分的宽度和高度
int width = right - x;
int height = top - y;
return width * height; // 返回面积交
}
int main() {
Rectangle rect1 = {1, 1, 4, 4}; // 第一个矩形
Rectangle rect2 = {3, 3, 4, 4}; // 第二个矩形
int intersectionArea = getIntersectionArea(rect1, rect2); // 计算矩形面积交
printf("矩形的面积交为: %d\n", intersectionArea);
return 0;
}
```
上述代码定义了一个Rectangle结构体用来表示矩形的左下角坐标、宽度和高度,并实现了一个getIntersectionArea函数来计算两个矩形的面积交。在main函数中,我们创建了两个矩形,并调用getIntersectionArea函数来计算它们的面积交,并将结果打印输出。
请注意,上述代码中的矩形坐标只是示例,您可以根据实际需求修改矩形的坐标来计算不同的矩形面积交。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)