题目链接:1018 锤子剪刀布
代码一、按照题目意思,依次判断即可
N = int(input())
ac,aj,ab,bc,bj,bb,ping = 0,0,0,0,0,0,0
for _ in range(N):
a,b = input().split()
if a == 'C' and b == 'J':
ac += 1
if a == 'J' and b == 'B':
aj += 1
if a == 'B' and b == 'C':
ab += 1
if b == 'C' and a == 'J':
bc += 1
if b =='J' and a == 'B':
bj += 1
if b == 'B' and a == 'C':
bb += 1
if a == b:
ping += 1
print('{} {} {}'.format((ab+ac+aj),ping,(bc+bj+bb)))
print('{} {} {}'.format((bc+bj+bb),ping,(ab+ac+aj)))
if ab >= aj and ab >= ac:
print('B',end=' ')
elif ac >= aj and ac >= ab:
print('C',end=' ')
elif aj >= ab and aj >= ac:
print('J',end=' ')
if bb >= bj and bb >= bc:
print('B',end='')
elif bc >= bj and bc >= bb:
print('C',end='')
elif bj >= bb and bj >= bc:
print('J',end='')
方法二、
(1)使用数组记录B、C、J赢得次数,由于有多个相同结果,按照字母顺序输出,所以下标为0、1、2
(2)在输入结束之后,根据最大值得下标输出B或者C或者J
N = int(input())
cnt_a = [0,0,0]
cnt_b = [0,0,0]
ping = 0
for _ in range(N):
a,b = input().split()
if a == 'B' and b == 'C':
cnt_a[0] += 1
if a == 'C' and b == 'J':
cnt_a[1] += 1
if a == 'J' and b == 'B':
cnt_a[2] += 1
if b == 'B' and a == 'C':
cnt_b[0] += 1
if b == 'C' and a == 'J':
cnt_b[1] += 1
if b =='J' and a == 'B':
cnt_b[2] += 1
if a == b:
ping += 1
print('{} {} {}'.format(sum(cnt_a),ping,sum(cnt_b)))
print('{} {} {}'.format(sum(cnt_b),ping,sum(cnt_a)))
dic = {0:'B',1:'C',2:'J'}
print('{} {}'.format(dic[cnt_a.index(max(cnt_a))],dic[cnt_b.index(max(cnt_b))]))
方法三、进一步优化,把赢和输的组合列举出来,分别用字典对应不同的值。
N = int(input())
cnt_a = [0,0,0]
cnt_b = [0,0,0]
dict_win={'B':0,'C':1,'J':2}
dict_lose={'C':0,'J':1,'B':2}
ping = 0
for _ in range(N):
a,b = input().split()
if a == b:
ping += 1
elif dict_win[a] == dict_lose[b]:
cnt_a[dict_win[a]] += 1
elif dict_win[b] == dict_lose[a]:
cnt_b[dict_win[b]] += 1
print('{} {} {}'.format(sum(cnt_a),ping,sum(cnt_b)))
print('{} {} {}'.format(sum(cnt_b),ping,sum(cnt_a)))
dic = {0:'B',1:'C',2:'J'}
print('{} {}'.format(dic[cnt_a.index(max(cnt_a))],dic[cnt_b.index(max(cnt_b))]))