Pat乙级1018题——锤子剪刀布(Python)

  • 注意是要计算获胜次数最多的手势,而不是出的次数最多的手势。

代码如下,可以试着将代码长度缩短。

def cjb():
    count = int(raw_input())
    a = []
    b = []
    for i in range(count):
        str = raw_input()
        a.append(str[0])
        b.append(str[-1])
    aCount = 0
    eqCount = 0
    aVic = []
    bVic = []
    for i in range(len(a)):
        if a[i] == b[i]:
            eqCount += 1
            continue
        if a[i] == 'C':
            if b[i] == 'J':
                aVic.append('C')
                aCount += 1
            else:
                bVic.append('B')
        elif a[i] == 'B':
            if b[i] == 'C':
                aVic.append('B')
                aCount += 1
            else:
                bVic.append('J')
        else:
            if b[i] == 'B':
                aVic.append('J')
                aCount += 1
            else:
                bVic.append('C')

    def listCount(list):
        maxCount = list.count('B')
        max = 'B'
        if maxCount < list.count('C'):
            maxCount = list.count('C')
            max = 'C'
        if maxCount < list.count('J'):
            maxCount = list.count('J')
            max = 'J'
        return max

    bCount = count - aCount - eqCount
    print '%d %d %d'%(aCount, eqCount, bCount)
    print '%d %d %d'%(bCount, eqCount, aCount)
    print '%s %s'%(listCount(aVic), listCount(bVic))

if __name__ == '__main__':
    cjb()
展开阅读全文

没有更多推荐了,返回首页