第一行包含一个数字 t (1 <= t <= 10)
接下来的t行每行包括四个数字 n, k, d1, d2(1 <= n <= 10^12; 0 <= k <= n, 0 <= d1, d2 <= k)
每行的比分数据,最终三只球队若能够打平,则输出“yes”,否则输出“no”
2 3 3 0 0 3 3 3 3
yes no
case1: 球队1和球队2 差0分,球队2 和球队3也差0分,所以可能的赛得分是三只球队各得1分
case2: 球队1和球队2差3分,球队2和球队3差3分,所以可能的得分是 球队1得0分,球队2得3分, 球队3 得0分,比赛已经全部结束因此最终不能打平。
t = int(input())
while t:
t -= 1
n, k, d1, d2 = list(map(int, input().split(' ')))
diff = n-k
a1,a2,a3,a4 = 2*d1+d2,2*d2+d1,2*d2-d1,d1+d2
a5 = 2*d1 -d2
ans = False
if diff >= a1 and (diff-a1) % 3 == 0 and k >= a2 and (k-a2)% 3 == 0:
ans = True
if diff >= a2 and (diff-a2) % 3 == 0 and k >= a1 and (k-a1) % 3 == 0:
ans = True
if diff >= a5 and (diff-a5) % 3 == 0 and k>= a4 and (k-a4) % 3 == 0:
ans = True
if diff >= a4 and (diff-a4) % 3 == 0 and k>= a5 and (k-a5) % 3 == 0:
q = (k-a5) // 3
if q + d1 - d2 >= 0:
ans = True
if ans:
print("yes")
else:
print("no")
分四种情况讨论,首先判断当前分数属于哪种情况(即求解出当前各队分数均大于等于0),然后计算对应情况下能否打平。
# 分析:
# 总分一定是n,三个队伍如果要平局,即 n%3=0;
# 剩余比赛场次:n-k场,如果要平局 则要当前分数落后的得分即可,并且n-k>=d1+d2;
# 四种情况:假设三个队伍的得分分别为q1,q2,q3且q1=m。
# 1、q1<q2 q2<q3:q1=m,q2=m+d1,q3=m+d1+d2;此时q3最大;若要平局:则需q1得分d1+d2,q2得分d2.至少d1+2*d2场,(n-k)>=(d1+2*d2)。
# 2、q1<q2 q2>q3:q1=m,q2=m+d1,q3=m+d1-d2;此时q2最大;若要平局;则需q1得分d1,q3得分d2.至少d1+d2场,(n-k)>=(d1+d2)
# 3、q1>q2 q2<q3:q1=m,q2=m-d1,q3=m-d1+d2;此时q2最低,此时(1)当d1>d2,q1最大,若要平局,需q2得分d1,q3得分d2-d1.至少d2场,(n-k)>=d2。
# (2)当d1<d2,q3最大。若要平局,需q1得分d2-d1,q2得分d2,至少2*d2-d1场,(n-k)>=(2*d2-d1)
# 4、q1>q2 q2>q3:q1=m,q2=m-d1,q3=m-d1-d2;此时q1最大;若要平局,则需q2得分d1,q3得分d1+d2,至少2*d1+d2场 (n-k)>=(2*d1+d2)
t = int(input())
while t:
t -= 1
s = [int(i) for i in input().split(" ")]
n = s[0]
k = s[1]
d1 = s[2]
d2 = s[3]
if d1 > n / 3 or d2 > n / 3 or(n - k) < 0:
print('no')
continue
if (n - k) < (d1 + d2) or n % 3 != 0:
print('no')
continue
remain = (n - k)
# case 1:
need_num1=(d1 + 2 * d2)
if remain >= need_num1 and (remain - need_num1) % 3 == 0:
print('yes')
continue
# case 2
need_num2 = (d1 + d2)
if remain >= need_num2 and (remain - need_num2) % 3 == 0:
print('yes')
continue
# case 3
need_num3_1 = d2
need_num3_2 = (2 * d2 - d1)
if (remain >= need_num3_1 and (remain - need_num3_1) % 3 == 0)&nbs***bsp;(remain >= need_num3_2 and (remain - (need_num3_2))% 3 == 0):
print('yes')
continue
# case 4
need_num3_4 = (2 * d1 + d2)
if remain >= need_num3_4 and (remain - need_num3_4) % 3 == 0:
print('yes')
continue
print("no")
def solution(t, arr):
for i in range(t):
if arr[i][0] % 3 ==0:
diff = arr[i][0] - arr[i][1]
a, b = max(arr[i][2], arr[i][3]), min(arr[i][2], arr[i][3])
c, d = arr[i][2], arr[i][3]
if diff >= (2c+d) and (diff - 2c - d) % 3 == 0 and (c+2d)<=arr[i][1]:
print("yes")
elif diff >= (c+2d) and (diff - c - 2d) % 3 == 0 and (2c+d)<=arr[i][1]:
print("yes")
elif diff >= (c+d) and (diff - c - d) % 3 == 0 and (2a-b)<= arr[i][1]:
print("yes")
elif diff >= (2a - b) and (diff - 2*a + b) % 3 == 0 and (c+d)<=arr[i][1]:
print("yes")
else:
print("no")
else:
print("no")
if name == 'main':
t = int(input())
arr = []
for i in range(t):
arr.append(list(map(int, input().split())))
solution(t, arr)
40%,回头再看看
def judge_score(scores):
total_score, comp_count, diff_12, diff_23 = scores[0], scores[1], scores[2], scores[3]
# 若总分不能被 3 整除,则一定不可能打平
if total_score % 3 != 0:
return False
flag_1, flag_2, flag_3, flag_4 = False, False, False, False
if (comp_count + (-diff_12 - diff_23)) % 3 == 0:
score_2 = (comp_count - (diff_12 + diff_23)) / 3
score_1 = score_2 + diff_12
score_3 = score_2 + diff_23
if score_2 >= 0:
flag_1 = True if max([score_1, score_3]) <= total_score / 3 else False
if (comp_count + (-diff_12 + diff_23)) % 3 == 0:
score_2 = (comp_count + (-diff_12 + diff_23)) / 3
score_1 = score_2 + diff_12
score_3 = score_2 - diff_23
if score_3 >= 0:
flag_2 = True if score_1 <= total_score / 3 else False
if (comp_count + (diff_12 - diff_23)) % 3 == 0:
score_2 = (comp_count + (diff_12 - diff_23)) / 3
score_1 = score_2 - diff_12
score_3 = score_2 + diff_23
if score_1 >= 0:
flag_3 = True if score_3 <= total_score / 3 else False
if (comp_count + (diff_12 + diff_23)) % 3 == 0:
score_2 = (comp_count + (diff_12 + diff_23)) / 3
score_1 = score_2 - diff_12
score_3 = score_2 - diff_23
if score_1 >= 0 and score_3 >= 0:
flag_4 = True if score_2 <= total_score / 3 else False
if flag_1 or flag_2 or flag_3 or flag_4:
return True
else:
return False
if __name__ == '__main__':
rows = int(input())
score_list = []
for i in range(rows):
score_list.append(list(map(int, input().split(' '))))
for scores in score_list:
if judge_score(scores):
print('yes')
else:
print('no') t = int(input())
res = []
for i in range(t):
n, k, d1, d2 =[int(x) for x in input().split()]
if n % 3 != 0:
res.append('no')
continue
## 求出三个球队的得分
if (k+2*d1+d2)%3 == 0 and (k-d1+d2)%3 == 0:
x = (k+2*d1+d2) /3
y = (k-d1+d2) / 3
z = k - x - y
if (x >= 0 and y >= 0 and z >= 0):
score1 = x
score2 = y
score3 = z
max_score = max(score1, score2, score3)
dif1 = max_score - score1
dif2 = max_score - score2
dif3 = max_score - score3
if dif1 + dif2 + dif3 <= n - k :
res.append('yes')
continue
if (k-2*d1+d2)%3 == 0 and (k+d1+d2)%3 == 0:
x = (k-2*d1+d2) /3
y = (k+d1+d2) / 3
z = k - x - y
if (x >= 0 and y >= 0 and z >= 0):
score1 = x
score2 = y
score3 = z
max_score = max(score1, score2, score3)
dif1 = max_score - score1
dif2 = max_score - score2
dif3 = max_score - score3
if dif1 + dif2 + dif3 <= n - k :
res.append('yes')
continue
if (k+2*d1-d2)%3 == 0 and (k-d1-d2)%3 == 0:
x = (k+2*d1-d2) /3
y = (k-d1-d2) / 3
z = k - x - y
if (x >= 0 and y >= 0 and z >= 0):
score1 = x
score2 = y
score3 = z
max_score = max(score1, score2, score3)
dif1 = max_score - score1
dif2 = max_score - score2
dif3 = max_score - score3
if dif1 + dif2 + dif3 <= n - k :
res.append('yes')
continue
if (k-2*d1-d2)%3 == 0 and (k+d1-d2)%3 == 0:
x = (k-2*d1-d2) /3
y = (k+d1-d2) / 3
z = k - x - y
if (x >= 0 and y >= 0 and z >= 0):
score1 = x
score2 = y
score3 = z
max_score = max(score1, score2, score3)
dif1 = max_score - score1
dif2 = max_score - score2
dif3 = max_score - score3
if dif1 + dif2 + dif3 <= n - k :
res.append('yes')
continue
res.append('no')
for i in range(len(res)):
print(res[i])