1 条题解
-
0
P76043 三点距离平方和 — 题解
专题: 函数
适用题目: 新生 OJ 本地题包中的 P76043,不要直接用于外站同名题。解题思路
封装两点距离平方
(x1-x2)^2+(y1-y2)^2,分别计算 AB、BC、CA 并相加。复杂度
时间和额外空间 。
易错点
距离平方不是欧氏距离,不要开平方;乘法最好用 64 位整数。
C11 参考程序
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <limits.h> long long dist2(long long x1,long long y1,long long x2,long long y2){ long long dx=x1-x2,dy=y1-y2; return dx*dx+dy*dy; } int main(void) { long long x[3],y[3]; for(int i=0;i<3;i++)scanf("%lld%lld",&x[i],&y[i]); long long ans=0; for(int i=0;i<3;i++){ int j=(i+1)%3; ans+=dist2(x[i],y[i],x[j],y[j]); } printf("%lld\n",ans);return 0; }C++17 参考程序
#include <bits/stdc++.h> using namespace std; long long dist2(long long x1, long long y1, long long x2, long long y2) { long long dx = x1 - x2, dy = y1 - y2; return dx * dx + dy * dy; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long x[3], y[3]; for (int i = 0; i < 3; i++) cin >> x[i] >> y[i]; long long ans = 0; for (int i = 0; i < 3; i++) { int j = (i + 1) % 3; ans += dist2(x[i], y[i], x[j], y[j]); } cout << ans << '\n'; return 0; }
题解建议设置为训练结束后开放。普通判题无法验证是否实际使用指定函数或 STL。
- 1
信息
- ID
- 1812
- 时间
- 2000ms
- 内存
- 256MiB
- 难度
- 1
- 标签
- 提交数
- 1
- 已通过
- 1
- 上传者