首页 > 试题广场 >

编程题1

[编程题]编程题1
  • 热度指数:25973 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
有三只球队,每只球队编号分别为球队1,球队2,球队3,这三只球队一共需要进行 n 场比赛。现在已经踢完了k场比赛,每场比赛不能打平,踢赢一场比赛得一分,输了不得分不减分。已知球队1和球队2的比分相差d1分,球队2和球队3的比分相差d2分,每场比赛可以任意选择两只队伍进行。求如果打完最后的 (n-k) 场比赛,有没有可能三只球队的分数打平。



输入描述:
第一行包含一个数字 t (1 <= t <= 10)
接下来的t行每行包括四个数字 n, k, d1, d2(1 <= n <= 10^12; 0 <= k <= n, 0 <= d1, d2 <= k)


输出描述:
每行的比分数据,最终三只球队若能够打平,则输出“yes”,否则输出“no”
示例1

输入

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),然后计算对应情况下能否打平。
发表于 2021-05-13 22:31:03 回复(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")

编辑于 2021-01-24 15:13:27 回复(1)
#分析详见绅士喵

t1 =  int(input())
t2 = [input() for x in range(0,t1)]
t3 = []
for y in t2:
    t4 = (list(map(int,y.split())))
    n =  t4[0]
    k = t4[1]
    d1 = t4[2]
    d2 = t4[3]
    #case1 m1<m2<m3
    if n % 3 != 0:
        t3.append('no')
    else:
        #case1 m1<m2<m3
        tmp =  k -2*d1 - d2
        if tmp >=0 and tmp %3 ==0:
            left = (n - k ) - (d1 + 2 *d2)
            if left>=0 and left % 3 ==0:
                t3.append('yes')
                continue
        #case2 m1<m2,m2>m3
        tmp =  k -2*d1 + d2
        if tmp >=0 and tmp %3 ==0:
            left = (n - k ) - (d1 + d2)
            if left>=0 and left % 3 ==0:
                t3.append('yes')
                continue
        #case3 m1>m2,m2>m3
        tmp =  k + 2*d1 + d2
        if tmp >=0 and tmp %3 ==0:
            left = (n - k ) - (2*d1 + d2)
            if left>=0 and left % 3 ==0:
                t3.append('yes')
                continue
        #case4-1 d1>d2,m1>m2,m2<m3,m1>m3
        tmp =  k + 2*d1 - d2
        if tmp >=0 and tmp %3 ==0:
            #case4-1 d1>=d2,m1>m2,m1<m3
            if d1 >=d2:
                left = (n - k ) - (2*d1 - d2)
                if left>=0 and left % 3 ==0:
                    t3.append('yes')
                    continue
            #case4-2 d1<d2,m1>m2,m2<m3,m1<m3
            else:
                left = (n - k ) - (2*d2 - d1)
                if left>=0 and left % 3 ==0:
                    t3.append('yes')
                    continue
        t3.append('no')

for x2 in t3:
    print(x2)
发表于 2020-08-14 16:37:58 回复(0)

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+2
d) 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 >= (2
a - 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)

发表于 2020-08-11 17:39:32 回复(0)
# 第一行包含一个数字 t (1 <= t <= 10)
# 接下来的t行每行包括四个数字 n, k, d1, d2(1 <= n <= 10^12; 0 <= k <= n, 0 <= d1, d2 <= k)
t = int(input())
res = []
for i in range(t):
    n,k,d1,d2 = input().split()
    for x in range(0,int(k)+1):
        for y in range(0,int(k)+1-x):
            if (abs(x-y)==int(d1)) and (abs(2*y+x-int(k))==int(d2)):
                if (x > int((int(n)/3))) or (y > int((int(n)/3)))or (int(k)-x-y > int((int(n)/3))):
                    res.append("no")
                else:
                    res.append("yes")
for i in range(t):
    try:
        print(res[i])
    except:
        print("no")
40%,回头再看看

发表于 2019-06-17 16:42:41 回复(0)
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')

编辑于 2019-03-23 13:21:38 回复(0)
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])

发表于 2019-02-17 18:04:01 回复(0)