def sort(data):
n = len(data)
for i in range(0, n-1):
for j in range(0, n-i-1):
if data[j] > data[j+1]:
temp = data[j]
data[j] = data[j+1]
data[j+1] = temp
return(data)
T = int(input())
for _ in range(T):
N = int(input())
a_i = list(map(int, input().split()))
if N < 3:
print(0)
else:
a_i = sort(a_i)
count = 0
for i in range(N-2):
for j in range(i+1,N-1):
for k in range(j+1,N):
if (a_i[i]^2) + (a_i[j]^2) == a_i[k]^2:
count += 1
print(count)
T = int(input())
for _ in range(T):
N = int(input())
a_i = list(map(int, input().split()))
if N < 3:
print(0)
else:
a_i.sort()
count = 0
for i in range(N-2):
for j in range(i+1,N-1):
for k in range(j+1,N):
if (a_i[i]^2) + (a_i[j]^2) == a_i[k]^2:
count += 1
print(count)
數字有可能是重複的,測資有可能是 3 3 3 4 4 4 5 5 5 這種形式
可以紀錄一下數字出現的頻率,然後直接用集合去重後再 sorted()
這樣就可以過了
數字有可能是重複的,測資有可能是 3 3 3 4 4 4 5 5 5 這種形式
可以紀錄一下數字出現的頻率,然後直接用集合去重後再 sorted()
這樣就可以過了
過了,
太感謝了
T = int(input())
for _ in range(T):
N = int(input())
a_i = list(map(int, input().split()))
a = list(set(sorted(a_i)))
times = []
count = 0
for i in a:
times.append(a_i.count(i))
if N < 3:
print(0)
elif len(times) == 3:
if (a[0]^2) + (a[1]^2) == a[2]^2:
count = times[0] * times[1] * times[2]
else:
N = len(a)
for i in range(N-2):
for j in range(i+1,N-1):
for k in range(j+1,N):
if a[i]**2 + a[j]**2 == a[k]**2:
count += times[i] * times[j] * times[k]
print(count)