输入有两行:第一行为N,3≤ N ≤256第二行为输入N个双字节整型坐标点,共2N个数据,以空格分隔横纵坐标及不同的点,x1 y1 x2 y2 … xn yn… xN yN
输出直角三角形个数
20 0 0 0 3 1 2 3 4 5 6 7 8 1 4 2 4 3 5 5 0 5 5 2 0 2 2 3 0 3 3 4 5 6 1 3 7 4 0 5 2
165
提示:1、不要重复计算三角形的个数,2、判断直角三角形用勾股定理
python代码最后一个超时
def distance(x, y, nums, dis): if x in dis: if y in dis[x]: return dis[x][y] else: dis[x] = {} dis[x][y] = (nums[x][0] - nums[y][0]) ** 2 + (nums[x][1] - nums[y][1]) ** 2 return dis[x][y] def count(nums): c = 0 dis = {} for x in range(len(nums)): for y in range(x + 1, len(nums)): for z in range(y + 1, len(nums)): edges = [distance(x, y, nums, dis), distance(x, z, nums, dis), distance(y, z, nums, dis)] max_dis = max(edges) if sum(edges) == 2 * max_dis: c += 1 return c if __name__ == "__main__": N = int(input()) nums = list(map(int, input().split(" "))) nums = [[nums[i * 2], nums[i * 2 + 1]] for i in range(N)] print(count(nums))
#include <bits/stdc++.h> using namespace std; long long distance(pair<int, int>A, pair<int, int>B) { int Ax = A.first; int Ay = A.second; int Bx = B.first; int By = B.second; long long X, Y; X = 1ll * (Bx - Ax) * (Bx - Ax); Y = 1ll * (By - Ay) * (By - Ay); return X + Y; } int main() { int N; cin >> N; vector<pair<int, int>> a(N); for (int i = 0; i < N; i++) { cin >> a[i].first >> a[i].second; } int count = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == j) continue; for (int k = 0; k < N; k++) { if (i == k || j == k) { continue; } long long Xij = distance(a[i], a[j]) ; long long Xjk = distance(a[j], a[k]) ; long long Xik = distance(a[i], a[k]) ; if (Xij == Xjk + Xik || Xjk == Xij + Xik || Xik == Xij + Xjk) { count++; } } } } cout << count / 6; } // 64 位输出请用 printf("%lld")