首页 > 试题广场 >

锤子剪刀布 (20)

[编程题]锤子剪刀布 (20)
  • 热度指数:31774 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
大家应该都会玩“锤子剪刀布”的游戏:

现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。

输入描述:
输入第1行给出正整数N(<=105),即双方交锋的次数。随后N行,每行给出一次交锋的信息,即甲、乙双方同时给出的的手势。C代表“锤子”、J代表“剪刀”、B代
表“布”,第1个字母代表甲方,第2个代表乙方,中间有1个空格。


输出描述:
输出第1、2行分别给出甲、乙的胜、平、负次数,数字间以1个空格分隔。第3行给出两个字母,分别代表甲、乙获胜次数最多的手势,中间有1个空格。如果解不唯
一,则输出按字母序最小的解。
示例1

输入

10<br/>C J<br/>J B<br/>C B<br/>B B<br/>B C<br/>C C<br/>C B<br/>J B<br/>B C<br/>J J

输出

5 3 2<br/>2 3 5<br/>B B
D = dict(
    zip(
        ["C C", "C J", "C B", "J C", "J J", "J B", "B C", "B J", "B B"],
        [0, 1, 2, 2, 0, 1, 1, 2, 0],
    )
)  # A win 1 A even 0 A defeated 2
N = eval(input())
L = [input() for i in range(N)]
a, b, c = (
    len([i for i in L if D[i] == 1]),
    len([i for i in L if D[i] == 0]),
    len([i for i in L if D[i] == 2]),
)
print(a, b, c)
print(c, b, a)
D2 = {}
for i in [i[0] for i in L if D[i] == 1]:
    D2[i] = 1 if i not in D2 else D2[i] + 1
D3 = {}
for i in [i[2] for i in L if D[i] == 2]:
    D3[i] = 1 if i not in D3 else D3[i] + 1
print(max(D2.keys(), key=lambda x: (D2[x], "JCB".index(x))),
      end=" ") if len(D2) else print("B", end=" ")
print(max(D3.keys(), key=lambda x: (D3[x], "JCB".index(x)))
     ) if len(D3) else print("B")

编辑于 2019-08-21 17:17:26 回复(0)
# -*- coding: utf-8 -*-
def function():
    N = int(input())
    count = {'C J': 0, 'J B': 0, 'B C': 0, 'C C': 0, 'J J': 0, 'B B': 0, 'J C': 0, 'B J': 0, 'C B': 0}
    regulation = {'C J': 1, 'J B': 1, 'B C': 1, 'C C': 1, 'J J': 1, 'B B': 1, 'J C': 1, 'B J': 1, 'C B': 1}
    for i in range(N):
        r = input()
        count[r] += regulation[r]
    gtcount = [('C J', count['C J']), ('J B', count['J B']), ('B C', count['B C'])]
    ltcount = [('C J', count['J C']), ('J B', count['B J']), ('B C', count['C B'])]
    gt = count['C J'] + count['J B'] + count['B C']
    eq = count['C C'] + count['B B'] + count['J J']
    lt = count['J C'] + count['B J'] + count['C B']
    gtcount.sort(key=lambda x: (-x[1], x[0]))
    ltcount.sort(key=lambda x: (-x[1], x[0]))
    print(gt, eq, lt, sep=' ')
    print(lt, eq, gt, sep=' ')
    print(gtcount[0][0].split()[0], ltcount[0][0].split()[0] )


if __name__ == '__main__':
    function()
发表于 2019-05-13 13:40:50 回复(0)
 
def result(x, y):
    a = [0, 0, 0] if (x == "C"): if (y == "C"):
            a[1] = 1  elif (y == "J"):
            a[0] = 1  else:
            a[2] = 1  elif (x == "J"): if (y == "C"):
            a[2] = 1  elif (y == "J"):
            a[1] = 1  else:
            a[0] = 1  else: if (y == "C"):
            a[0] = 1  elif (y == "J"):
            a[2] = 1  else:
            a[1] = 1  return a

epoch = input()
epoch = int(epoch)
a = [0, 0, 0]
winx = [0, 0, 0]
winy = [0, 0, 0] for i in range(1, epoch + 1):
    x, y = input().split() if result(x, y)[0] == 1: if (x == "B"):
            winx[0] = winx[0] + 1  elif (x == "C"):
            winx[1] = winx[1] + 1  else:
            winx[2] = winx[2] + 1  if result(x, y)[2] == 1: if (y == "B"):
            winy[0] = winy[0] + 1  elif (y == "C"):
            winy[1] = winy[1] + 1  else:
            winy[2] = winy[2] + 1  for j in range(3):
        a[j] = a[j] + result(x, y)[j] print(a[0], a[1], a[2]) print(a[2], a[1], a[0]) if (winx.index((max(winx))) == 0): print("B", end="") elif (winx.index((max(winx))) == 1): print("C", end="") else: print("J", end="") print(" ", end="") if (winy.index((max(winy))) == 0): print("B") elif (winy.index((max(winy))) == 1): print("C") else: print("J")
编辑于 2019-03-19 10:34:25 回复(1)
Time = input()
dictA = {"C":0,"J":0,"B":0,"T":0}
dictB = {"C":0,"J":0,"B":0,"T":0}
tie = 0
for i in range(int(Time)):
    guess = [i for i in input().split()]
    if guess[0] == guess[1]:
        tie += 1
    elif guess[0] == "C" and guess[1] == "J":
        dictA["C"] += 1
    elif guess[0] == "J" and guess[1] == "B":
        dictA["J"] += 1
    elif guess[0] == "B" and guess[1] == "C":
        dictA["B"] += 1
    elif guess[0] == "C" and guess[1] == "B":
        dictB["B"] += 1
    elif guess[0] == "J" and guess[1] == "C":
        dictB["C"] += 1
    elif guess[0] == "B" and guess[1] == "J":
        dictB["J"] += 1
winA = dictA["C"] + dictA["J"] + dictA["B"]
winB = dictB["C"] + dictB["J"] + dictB["B"]
mostA = "J" if dictA["J"] > dictA["C"] else "C"
mostA = "C" if dictA["C"] > dictA["B"] else "B"
mostB = "J" if dictB["J"] > dictB["C"] else "C"
mostB = "C" if dictB["C"] > dictB["B"] else "B"
print(winA,tie,int(Time)-winA-tie)
print(winB,tie,int(Time)-winB-tie)
print(mostA,mostB)

发表于 2019-03-08 09:07:28 回复(3)
n = int(input())
dict_jia = {"B":0,"C":0,"J":0,}
dict_yi = {"B":0,"C":0,"J":0,}
a1,a2,a3=0,0,0, for i in range(n):
    jia,yi = map(str,input().split()) if (jia == "C" and yi == "J") or (jia == "J" and yi == "B")or (jia == "B" and yi == "C"):
        a1 = a1 + 1  dict_jia[jia] = dict_jia[jia] + 1  elif jia == yi:
        a2 = a2 + 1  else:
        a3 = a3+1  dict_yi[yi] = dict_yi[yi] + 1 b1 = n - a1 - a2
b2 = a2
b3 = n - a3 -a2
list_jia = sorted(dict_jia.items(), key=lambda a:(-a[1],a[0]))
list_yi = sorted(dict_yi.items(), key=lambda a:(-a[1],a[0])) print(a1,a2,a3) print(b1,b2,b3) print(list_jia[0][0],list_yi[0][0])
发表于 2019-02-13 22:24:20 回复(0)

python solution

win, lose, tie = 0, 0, 0 #甲的勝、負、平次數
Jia, Yi = [0, 0, 0], [0, 0, 0]  # B C J  count #甲乙勝時出的手勢記錄
num = int(input())
for i in range(num):
    string = input()
    if string == "C J":
        win += 1
        Jia[1] += 1
    elif string == "J C":
        lose += 1
        Yi[1] += 1
    elif string == "C B":
        lose += 1
        Yi[0] += 1
    elif string == "B C":
        win += 1
        Jia[0] += 2
    elif string == "B J":
        lose += 1
        Yi[2] += 1
    elif string == "J B":
        win += 1
        Jia[2] += 1
    else:  # there is a tie
        tie += 1
print(win, tie, lose)
print(lose, tie, win)
print("BCJ"[Jia.index(max(Jia))], "BCJ"[Yi.index(max(Yi))])
发表于 2017-11-17 14:22:40 回复(0)
j_p = 0
j_s_c, j_s_j, j_s_b = 0, 0, 0
y_p = 0
y_s_c, y_s_j, y_s_b = 0, 0, 0


def most_win(b, c, j):
    if b == max(b, c, j):
        return 'B'
    elif c == max(b, c, j):
        return 'C'
    elif j == max(b, c, j):
        return 'J'


n = int(input())
for i in range(n):
    j, y = input().split()
    if j == y:
        j_p += 1  y_p += 1
    elif j == 'C' and y == 'J':
        j_s_c += 1
    elif j == 'J' and y == 'B':
        j_s_j += 1
    elif j == 'B' and y == 'C':
        j_s_b += 1
    elif y == 'B' and j == 'C':
        y_s_b += 1
    elif y == 'C' and j == 'J':
        y_s_c += 1
    elif y == 'J' and j == 'B':
        y_s_j += 1

j_s = j_s_b + j_s_c + j_s_j
j_f = n - j_p - j_s
y_s = y_s_b + y_s_c + y_s_j
y_f = n - y_p - y_s
j_s_most = most_win(j_s_b, j_s_c, j_s_j)
y_s_most = most_win(y_s_b, y_s_c, y_s_j)

print(j_s, j_p, j_f)
print(y_s, y_p, y_f)
print(j_s_most, y_s_most)

编辑于 2017-11-14 14:57:20 回复(0)
# 0、1、2分别表示第一个人胜、平、负
def CJB(s1, s2):
    if s1 == 'C' and s2 == 'J':
        return 0
    if s1 == 'C' and s2 == 'C':
        return 1
    if s1 == 'C' and s2 == 'B':
        return 2
    if s1 == 'J' and s2 == 'B':
        return 0
    if s1 == 'J' and s2 == 'J':
        return 1
    if s1 == 'J' and s2 == 'C':
        return 2
    if s1 == 'B' and s2 == 'C':
        return 0
    if s1 == 'B' and s2 == 'B':
        return 1
    if s1 == 'B' and s2 == 'J':
        return 2
 
from collections import Counter
 
try:
    while True:
        f_vNum, f_dNum, f_mNum = 0, 0, 0      # f_vNum, f_dNum, f_mNum分别表示第一个人胜、平、负的次数
        fv, sv = [], []
        n = input()
        for i in range(n):
            tmp = raw_input().split()
            if CJB(tmp[0], tmp[1]) == 0:
                f_vNum += 1
                fv.append(tmp[0])
            if CJB(tmp[0], tmp[1]) == 1:
                f_dNum += 1
            if CJB(tmp[0], tmp[1]) == 2:
                f_mNum += 1
                sv.append(tmp[1])
 
        fCounter = Counter(fv).most_common()
        sCounter = Counter(sv).most_common()
        fCounter.sort()
        sCounter.sort()
        if fv:
            fMost = fCounter[0][0]
        else:
            fMost = 'B'
        if sv:
            sMost = sCounter[0][0]
        else:
            sMost = 'B'
        print f_vNum, f_dNum, f_mNum
        print f_mNum, f_dNum, f_vNum
        print fMost, sMost
except EOFError:
    pass
人生苦短,我用Python
发表于 2016-11-03 16:19:15 回复(0)
# 0、1、2分别表示第一个人胜、平、负
def CJB(s1, s2):
    if s1 == 'C' and s2 == 'J':
        return 0
    if s1 == 'C' and s2 == 'C':
        return 1
    if s1 == 'C' and s2 == 'B':
        return 2
    if s1 == 'J' and s2 == 'B':
        return 0
    if s1 == 'J' and s2 == 'J':
        return 1
    if s1 == 'J' and s2 == 'C':
        return 2
    if s1 == 'B' and s2 == 'C':
        return 0
    if s1 == 'B' and s2 == 'B':
        return 1
    if s1 == 'B' and s2 == 'J':
        return 2
from collections import Counter
try:
    while True:
        f_vNum, f_dNum, f_mNum = 0, 0, 0      # f_vNum, f_dNum, f_mNum分别表示第一个人胜、平、负的次数
        fv, sv = [], []
        n = input()
        for i in range(n):
            tmp = raw_input().split()
            if CJB(tmp[0], tmp[1]) == 0:
                f_vNum += 1
                fv.append(tmp[0])
            if CJB(tmp[0], tmp[1]) == 1:
                f_dNum += 1
            if CJB(tmp[0], tmp[1]) == 2:
                f_mNum += 1
                sv.append(tmp[1])
        fCounter = Counter(fv).most_common()
        sCounter = Counter(sv).most_common()
        fCounter.sort()
        sCounter.sort()
        if fv:
            fMost = fCounter[0][0]
        else:
            fMost = 'B'
        if sv:
            sMost = sCounter[0][0]
        else:
            sMost = 'B'
        print f_vNum, f_dNum, f_mNum
        print f_mNum, f_dNum, f_vNum
        print fMost, sMost
except EOFError:
    pass
人生苦短,我用Python
发表于 2016-11-01 23:21:26 回复(0)

问题信息

难度:
9条回答 25705浏览

热门推荐

通过挑战的用户

锤子剪刀布 (20)