本文介绍: CCF模拟题 202303-1田地丈量

问题描述
试题编号: 202303-1
试题名称: 田地丈量
时间限制: 1.0s
内存限制: 512.0MB

问题描述:
西西艾弗岛上散落着 n 块田地。每块田地可视为平面直角坐标系下的一块矩形区域,由左下角坐标 (x1,y1) 和右上角坐标 (x2,y2) 唯一确定,且满足 x1<x2、y1<y2。这 n 块田地中,任意两块的交集面积均为 0,仅边界处可能有所重叠。
最近,顿顿想要在南山脚下开垦出一块面积为 a×b 矩形田地,其左下角坐标为 (0,0)、右上角坐标为 (a,b)。试计算顿顿选定区域内已经存在的田地面积。

输入格式:
从标准输入读入数据。
输入共 n+1 行。
输入的第一行包含空格分隔的三个正整数 n、a 和 b,分别表示西西艾弗岛上田地块数和顿顿选定区域的右上角坐标。
接下来 n 行,每行包含空格分隔的四个整数 x1、y1、x2 和 y2,表示一块田地的位置。

输出格式:
输出到标准输出。
输出一个整数,表示顿顿选定区域内的田地面积。

样例输入

4 10 10
0 0 5 5
5 -2 15 3
8 8 15 15
-2 10 3 15

样例输出

44

子任务
全部的测试数据满足 n<=100,且所有输入坐标的绝对值均不超过 10的4次方。

Java 代码:

// CCF_2023_03_1
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int a = scanner.nextInt();
        int b = scanner.nextInt();

        int totalArea = 0;
        for (int i = 0; i < n; i++) {
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();

            // 判断田地是否在顿顿选定区域内
            if (x1 < a && y1 < b && x2 > 0 && y2 > 0) {
                // 计算田地面积并加到总面积上
                totalArea += Math.max(0, Math.min(x2, a) - Math.max(x1, 0)) * Math.max(0, Math.min(y2, b) - Math.max(y1, 0));
            } else if (x1 >= a && y1 >= b) {
                // 田地完全在顿顿选定区域外,不计入面积
            } else {
                // 计算田地在顿顿选定区域内的面积
                int x3 = Math.max(x1, 0);
                int y3 = Math.max(y1, 0);
                int x4 = Math.min(x2, a);
                int y4 = Math.min(y2, b);
                if (x3 < x4 && y3 < y4) {
                    totalArea += (x4 - x3) * (y4 - y3);
                }
            }
        }

        System.out.println(totalArea);
    }
}

C语言 代码:

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, a, b;
    scanf("%d %d %d", &n, &a, &b);

    int totalArea = 0;
    for (int i = 0; i < n; i++) {
        int x1, y1, x2, y2;
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

        // 判断田地是否在顿顿选定区域内
        if (x2 <= a && y2 <= b && x1 >= 0 && y1 >= 0) {
            // 计算田地面积并加到总面积上
            totalArea += (x2 - x1) * (y2 -y1);
        } else if (x1 > a && y1 > b) {
            // 田地完全在顿顿选定区域外,不计入面积
        } else {
            // 计算田地在顿顿选定区域内的面积
            int x3 = x1 > 0 ? x1 : 0;
            int y3 = y1 > 0 ? y1 : 0;
            int x4 = x2 < a ? x2 : a;
            int y4 = y2 < b ? y2 : b;
            if (x3 < x4 && y3 < y4) {
                totalArea += (x4 - x3) * (y4 - y3);
            }
        }
    }

    printf("%dn", totalArea);
    return 0;
}

原文地址:https://blog.csdn.net/myec_lipse/article/details/135474406

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_54176.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注