Squares 四边形数量,求坐标很重要

博客围绕根据夜空中星星坐标求可形成四边形数量的问题展开。给出问题描述、输入输出要求及示例,还强调标记数组要用 boolean 类型以避免超内存,最后提供了实现该功能的 C++ 代码。
Problem Description
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property. 

So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates. 
 

 

Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
 

 

Output
For each test case, print on a line the number of squares one can form from the given stars.
 

 

Sample Input
4 1 0 0 1 1 1 0 0 9 0 0 1 0 2 0 0 2 1 2 2 2 0 1 1 1 2 1 4 -2 5 3 7 0 0 5 2 0
 

 

Sample Output
1 6 1
***************************************************************************************************************************
记着标记数组要用boolean类型
***************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #include<cstdio>
 7 using namespace std;
 8 int x[1010],y[1010];
 9 bool fs[5000][5000];//此处要定义成bool类型的,否则超内存
10 int a,b,c,i,j,sum,n;
11 int f(int a,int b)
12 {
13       if(fs[a+2500][b+2500])
14          return 1;
15      return 0;
16 }
17 
18 int main()
19 {
20     int x1,y1,x2,y2,x3,y3,x4,y4;
21     int a,b,i,j,sum,n;
22     while(scanf("%d",&n)&&n)
23     {
24         sum=0;
25         memset(fs,0,sizeof(fs));
26         for(i=0;i<n;i++)
27         {
28             scanf("%d %d",&a,&b);
29             x[i]=a;
30             y[i]=b;
31             fs[a+2500][b+2500]=1;
32         }
33         for(i=0;i<n;i++)
34         {
35              x1=x[i];
36              y1=y[i];
37             for(j=0;j<i;j++)
38             {
39                  x2=x[j];
40                  y2=y[j];
41                  x3,y3,x4,y4;
42                 x3=x1+(y1-y2);//这个公式推推就出来啦
43                 y3=y1-(x1-x2);
44                 x4=x2+(y1-y2);
45                 y4=y2-(x1-x2);
46                 if(f(x3,y3)&&f(x4,y4))
47                   sum++;
48                 x3=x1-(y1-y2);
49                 y3=y1+(x1-x2);
50                 x4=x2-(y1-y2);
51                 y4=y2+(x1-x2);
52                 if(f(x3,y3)&&f(x4,y4))
53                   sum++;
54             }
55         }
56         printf("%d\n",sum/4);//注意此处要除以4,
57 
58     }
59 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3389384.html

``` X1 = [1, 2, 3, 4, 5]; Y1 = [2, 4, 6, 8, 10]; % 真实模型:y=2x n1 = 1; X2 = 0:0.5:2; Y_true = 1 + 2*X2 + 3*X2.^2; % 真实模型:y=3x²+2x+1 rng(2025); % 固定随机种子 Y2 = Y_true + 0.5*randn(size(X2)); % 添加高斯噪声 n2 = 2; % 模拟温度传感器标定数据(非线性特性) X3 = [10, 20, 30, 40, 50, 60]; % 温度(℃) Y3 = [0.8, 1.1, 1.5, 2.0, 2.6, 3.3]; % 输出电压(V) n3 = 3; % 三阶多项式 % 测试组1 [a1, R2_1, MSE1] = my_least_squares(X1, Y1, n1); % 测试组2 [a2, R2_2, MSE2] = my_least_squares(X2, Y2, n2); % 测试组3 [a3, R2_3, MSE3] = my_least_squares(X3, Y3, n3); % 最小二乘法曲线拟合(多项式阶数n) % 输入:x-原始数据横坐标向量,y-原始数据纵坐标向量,n-多项式阶数 % 输出:拟合曲线方程、误差指标及对比图 function [A, MSE, R2] = my_least_squares(x, y, n) % 数据预处理 x = x(:); y = y(:); % 转换为列向量 m = length(x); % 数据点数量 % 构造设计矩阵X(维度m×(n+1)) X = zeros(m, n+1); for i = 1:n+1 X(:, i) = x.^(i-1); % 多项式基函数 end % 构造法方程组并解系数A XT = X'; % 转置矩阵 A = (XT * X) \ XT * y; % 解线性方程组 (X^T X)A = X^T y[4,6] % 计算拟合值 y_fit = X * A; % 计算误差指标 MSE = mean((y - y_fit).^2); % 均方误差 SST = sum((y - mean(y)).^2); % 总平方和 SSR = sum((y_fit - mean(y)).^2); % 回归平方和 R2 = SSR / SST; % 决定系数 % 绘制结果 x_fit = linspace(min(x), max(x), 100); % 生成密集点 y_fit_plot = polyval(flipud(A), x_fit); % 生成拟合曲线 figure; scatter(x, y, 'ro', 'DisplayName', '原始数据'); hold on; plot(x_fit, y_fit_plot, 'b-', 'LineWidth', 2, 'DisplayName', '拟合曲线'); xlabel('x'); ylabel('y'); title(sprintf('最小二乘拟合曲线 (n=%d, R²=%.4f)', n, R2)); legend('Location', 'best'); grid on; % 输出方程表达式 fprintf('拟合方程: y = '); for i = 1:n+1 if A(i) ~= 0 fprintf('%.4f x^{%d} + ', A(i), n+1-i); end end fprintf('\b\b \n'); end```给出该段代码的程序流程图
最新发布
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值