构成正方形的数量

news/2025/2/27 2:21:33

构成正方形的数量

真题目录: 点击去查看

E 卷 100分题型

题目描述

输入N个互不相同的二维整数坐标,求这N个坐标可以构成的正方形数量。[内积为零的的两个向量垂直]

输入描述

第一行输入为N,N代表坐标数量,N为正整数。N <= 100

之后的 K 行输入为坐标x y以空格分隔,x,y为整数,-10<=x, y<=10

输出描述

输出可以构成的正方形数量。

示例1

输入

3
1 3
2 4
3 1

输出

0

说明

(3个点不足以构成正方形)

示例2

输入

4
0 0
1 2
3 1
2 -1

输出

1

题解

思路:
利用的是正方形中的边的向量关系进行求解。假设A(x1, y1)和B(x2,y2)是正方形的两条边。 组成的向量AB为{x2- x1, y2 - y1}
注意向量旋转的两个公式:

逆时针旋转90°, (x, y) -> (-y, x)
顺时针旋转180°, (x, y) -> (y, -x)

情况一:(图画的不是很准确应该是正方形的)

BA向量的值为{x1-x2, y1-y2}, 逆时针旋转可以得出AD、BC向量的值 => x3 = x1 - (y1 - y2), y3 = y1 + (x1 - x2), x4=x2 - (y1 - y2), y4= y2 + (x1 - x2)

情况二:(图画的不是很准确应该是正方形的)
在这里插入图片描述

可以推导出x3 = x2 + (y1 - y2), y3=y2 - (x1 - x2), x4=x1+(y1-y2), y4=y3 - (x1 - x2)

根据上述推到过程,可以枚举两个点,然后去判断两外两个点是否存在来判断是否存在正方形。四个点两两组合,一共会组合出4次,所以最终结果 = 统计结果 / 4。

c++

#include <iostream>
#include <vector>
using namespace std;

// 定义一个点结构体
struct Point {
    int x, y;
};

// 判断两个点是否相等
bool arePointsEqual(Point a, Point b) {
    return a.x == b.x && a.y == b.y;
}

// 检查数组中是否存在某点
bool pointExists(vector<Point>& points, Point p) {
    for (auto& point : points) {
        if (arePointsEqual(point, p)) {
            return true;
        }
    }
    return false;
}

int main() {
    int n;
    cin >> n;

    vector<Point> points(n);

    // 读取坐标并存入数组
    for (int i = 0; i < n; i++) {
        cin >> points[i].x >> points[i].y;
    }

    int squareCount = 0;

    // 遍历所有点对,检查是否能构成正方形
    for (int i = 0; i < n; i++) {
        int x1 = points[i].x;
        int y1 = points[i].y;

        for (int j = i + 1; j < n; j++) {
            int x2 = points[j].x;
            int y2 = points[j].y;

            // 计算两个可能的对角点
            Point p3 = {x1 - (y1 - y2), y1 + (x1 - x2)};
            Point p4 = {x2 - (y1 - y2), y2 + (x1 - x2)};

            if (pointExists(points, p3) && pointExists(points, p4)) {
                squareCount++;
            }

            // 计算另外两个可能的对角点
            Point p5 = {x1 + (y1 - y2), y1 - (x1 - x2)};
            Point p6 = {x2 + (y1 - y2), y2 - (x1 - x2)};

            if (pointExists(points, p5) && pointExists(points, p6)) {
                squareCount++;
            }
        }
    }

    // 每个正方形被计算了4次,因此结果需要除以4
    cout << squareCount / 4 << endl;

    return 0;
}

Java

import java.util.*;

class Point {
    int x, y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // 判断两个点是否相等
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Point) {
            Point p = (Point) obj;
            return this.x == p.x && this.y == p.y;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }
}

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

        Set<Point> points = new HashSet<>();
        List<Point> pointList = new ArrayList<>();

        // 读取坐标并存入集合和列表
        for (int i = 0; i < n; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            Point p = new Point(x, y);
            points.add(p);
            pointList.add(p);
        }

        int squareCount = 0;

        // 遍历所有点对,检查是否能构成正方形
        for (int i = 0; i < n; i++) {
            Point p1 = pointList.get(i);

            for (int j = i + 1; j < n; j++) {
                Point p2 = pointList.get(j);

                // 计算两个可能的对角点
                Point p3 = new Point(p1.x - (p1.y - p2.y), p1.y + (p1.x - p2.x));
                Point p4 = new Point(p2.x - (p1.y - p2.y), p2.y + (p1.x - p2.x));

                if (points.contains(p3) && points.contains(p4)) {
                    squareCount++;
                }

                // 计算另外两个可能的对角点
                Point p5 = new Point(p1.x + (p1.y - p2.y), p1.y - (p1.x - p2.x));
                Point p6 = new Point(p2.x + (p1.y - p2.y), p2.y - (p1.x - p2.x));

                if (points.contains(p5) && points.contains(p6)) {
                    squareCount++;
                }
            }
        }

        // 每个正方形被计算了4次,因此结果需要除以4
        System.out.println(squareCount / 4);
        scanner.close();
    }
}

Python

import sys

# 读取输入
n = int(sys.stdin.readline().strip())
points = set()
point_list = []

# 读取坐标并存入集合和列表
for _ in range(n):
    x, y = map(int, sys.stdin.readline().strip().split())
    point = (x, y)
    points.add(point)
    point_list.append(point)

square_count = 0

# 遍历所有点对,检查是否能构成正方形
for i in range(n):
    x1, y1 = point_list[i]

    for j in range(i + 1, n):
        x2, y2 = point_list[j]

        # 计算两个可能的对角点
        p3 = (x1 - (y1 - y2), y1 + (x1 - x2))
        p4 = (x2 - (y1 - y2), y2 + (x1 - x2))

        if p3 in points and p4 in points:
            square_count += 1

        # 计算另外两个可能的对角点
        p5 = (x1 + (y1 - y2), y1 - (x1 - x2))
        p6 = (x2 + (y1 - y2), y2 - (x1 - x2))

        if p5 in points and p6 in points:
            square_count += 1

# 每个正方形被计算了4次,因此结果需要除以4
print(square_count // 4)

JavaScript

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const points = new Set();
const pointList = [];
let n;
let lineCount = 0;

rl.on("line", (line) => {
    if (lineCount === 0) {
        n = parseInt(line.trim());
    } else {
        const [x, y] = line.trim().split(" ").map(Number);
        const key = `${x},${y}`;
        points.add(key);
        pointList.push([x, y]);
    }

    lineCount++;

    if (lineCount > n) {
        rl.close();
    }
});

rl.on("close", () => {
    let squareCount = 0;

    // 遍历所有点对,检查是否能构成正方形
    for (let i = 0; i < n; i++) {
        const [x1, y1] = pointList[i];

        for (let j = i + 1; j < n; j++) {
            const [x2, y2] = pointList[j];

            // 计算两个可能的对角点
            const p3 = `${x1 - (y1 - y2)},${y1 + (x1 - x2)}`;
            const p4 = `${x2 - (y1 - y2)},${y2 + (x1 - x2)}`;

            if (points.has(p3) && points.has(p4)) {
                squareCount++;
            }

            // 计算另外两个可能的对角点
            const p5 = `${x1 + (y1 - y2)},${y1 - (x1 - x2)}`;
            const p6 = `${x2 + (y1 - y2)},${y2 - (x1 - x2)}`;

            if (points.has(p5) && points.has(p6)) {
                squareCount++;
            }
        }
    }

    // 每个正方形被计算了4次,因此结果需要除以4
    console.log(Math.floor(squareCount / 4));
});

Go

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

// 解析输入
func parseInput() ([][2]int, map[[2]int]bool) {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())

	points := make(map[[2]int]bool)
	pointList := make([][2]int, n)

	for i := 0; i < n; i++ {
		scanner.Scan()
		tokens := strings.Fields(scanner.Text())
		x, _ := strconv.Atoi(tokens[0])
		y, _ := strconv.Atoi(tokens[1])

		p := [2]int{x, y}
		points[p] = true
		pointList[i] = p
	}
	return pointList, points
}

func main() {
	pointList, points := parseInput()
	n := len(pointList)
	squareCount := 0

	// 遍历所有点对,检查是否能构成正方形
	for i := 0; i < n; i++ {
		x1, y1 := pointList[i][0], pointList[i][1]

		for j := i + 1; j < n; j++ {
			x2, y2 := pointList[j][0], pointList[j][1]

			p3 := [2]int{x1 - (y1 - y2), y1 + (x1 - x2)}
			p4 := [2]int{x2 - (y1 - y2), y2 + (x1 - x2)}

			if points[p3] && points[p4] {
				squareCount++
			}

			p5 := [2]int{x1 + (y1 - y2), y1 - (x1 - x2)}
			p6 := [2]int{x2 + (y1 - y2), y2 - (x1 - x2)}

			if points[p5] && points[p6] {
				squareCount++
			}
		}
	}

	fmt.Println(squareCount / 4)
}


http://www.niftyadmin.cn/n/5869384.html

相关文章

前端页面什么是全屏嵌入/什么是局部嵌入

1. 什么是 <div> 容器标签&#xff1f; 通俗来说&#xff0c;<div> 标签就像一个“盒子”&#xff0c;你可以把任何东西放进去&#xff0c;比如文字、图片、按钮等。它是一个非常灵活的标签&#xff0c;用来组织和分隔网页内容。 举个例子&#xff1a; 想象你有…

Redis面试题----MySQL 里有 2000w 数据,Redis 中只存 20w 的数据,如何保证 Redis 中的数据都是热点数据?

要保证 Redis 中存储的 20w 数据都是热点数据&#xff0c;可以从数据筛选和数据淘汰两个大的方面来考虑&#xff0c;以下是详细的实现思路和方法&#xff1a; 数据筛选 1. 基于业务规则 分析业务场景&#xff1a;不同的业务场景有不同的热点数据特征。例如&#xff0c;在电商…

数字IC低功耗后端设计实现之power gating和isolation技术

考虑低功耗设计需求&#xff0c;下图中间那个功能模块是需要做power domain的&#xff0c;即这个模块需要插MTCMOS。需要开启时&#xff0c;外面的VDD会和这个模块的LOCAL VDD形成通路&#xff0c;否则就是断开即power off状态。 这些低功耗设计实现经验&#xff0c;你真的懂了…

如何在 WPS 中集成 DeepSeek

如何在 WPS 中集成 DeepSeek&#xff1a;从零基础到高阶开发的完整指南 DeepSeek 作为国内领先的 AI 办公助手&#xff0c;与 WPS 的深度整合可显著提升文档处理效率。本文提供 ​4 种集成方案&#xff0c;覆盖从「小白用户」到「企业开发者」的全场景需求&#xff0c;并包含 …

flutter Running Gradle task ‘assembleDebug‘...

这个和单独的android还不太一样 Flutter 在Android studio运行时卡在Running Gradle task assembleDebug... - 简书

MFC文件和注册表的操作

MFC文件和注册表的操作 日志、操作配置文件、ini、注册表、音视频的文件存储 Linux下一切皆文件 C/C操作文件 const char* 与 char* const const char* 常量指针&#xff0c;表示指向的内容为常量。指针可以指向其他变量&#xff0c;但是内容不能再变了 char szName[6]&qu…

SQL笔记#函数、谓词、CASE表达式

目录 一、各种各样的函数 1、函数的种类 2、算术函数 ABS——绝对值 MOD——取余 ROUND——四舍五入 3、字符串函数 ||——拼接 LENGTH——字符串长度 LOWER——小写转换 REPLACE——字符串的替换 SUBSTR——字符串的截取 UPPER——大写转换 4、日期函数 CURRENT_DATE——…

leetcode day22 59

59 螺旋矩阵 给你一个正整数 n &#xff0c;生成一个包含 1 到 n2 所有元素&#xff0c;且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。 示例 1&#xff1a; 输入&#xff1a;n 3 输出&#xff1a;[[1,2,3],[8,9,4],[7,6,5]]示例 2&#xff1a; 输入&#xff1a;…