【蓝桥杯】codeforce 1272A.Three Friends

题目地址:https://codeforces.com/contest/1272/problem/A

题目描述

题目是英文的,机器翻译如下所示

image-20240412210615169

image-20240412210803355

1
2
3
4
5
6
7
8
9
10
# 输入
8
3 3 4
10 20 30
5 5 5
2 4 3
1 1000000000 1000000000
1 1000000000 999999999
3 2 5
3 2 6
1
2
3
4
5
6
7
8
9
# 输出
0
36
0
0
1999999994
1999999994
2
4

我的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
num = int(input())

def juli(num1,num2,num3):
return abs(num1-num2)+abs(num1-num3)+abs(num2-num3)

def baoli(a,b,c):
st = set()
for i in range(a-1,a+2):
for j in range(b-1,b+2):
for k in range(c-1,c+2):
st.add(juli(i,j,k))
return min(st)

res = []
for i in range(num):
lst = input().split(" ")
lst = list(map(int,lst))
a,b,c = lst[0],lst[1],lst[2]
res.append(baoli(a,b,c))

for x in res:
print(x)

别人的标准答案

1
2
3
4
5
6
7
8
9
10
11
t=int(input())
while t > 0:
t -= 1
a,b,c = map(int, input().split())
# float('inf')表示正无穷,数据类型为float
ans = float('inf')
for i in range(a-1,a+2):
for j in range(b-1,b+2):
for k in range(c-1,c+2):
ans = min(ans, abs(i-j)+abs(i-k)+abs(j-k))
print(ans)

提交方法

在右侧的Submit,language选择为Python3。选择文件为xxx.py文件,然后点击提交按钮

image-20240412211129525

通过

image-20240412211302375