Review of Codeforces 5A and 5B(Python)

来源:互联网 发布:电子相册视频制作软件 编辑:程序博客网 时间:2024/05/05 11:34

5A. Chat Server's Outgoing Traffic

Seems like a simple task. However, you may find it is difficult to tell the determination of the input. There are two method: first one is using sys.stdin. you may need to learn how to use sys model. The second method is using exeption method(try...catch)

source code as follows:

v, m = 0, 0while True:    try:        temp = raw_input()    except :        break    if temp[0] == '+':        m += 1    elif temp[0] == '-':        m -= 1    else:        i = temp.index(':')        v += len(temp[i+1::]) * mprint v

5B Center Alignment

if you have the exps of UI, this problem is easy to solve. also use try...catch to deal with the input, source code as follows:

v = []while True:    try:        s = raw_input()    except :        break    l = len(s)    v.append([s, l])vm = v[:]vm.sort(key = lambda x : x[1])width = vm[-1][1] + 2cc = 0print '*' * widthfor i in xrange(len(v)):    temp = v[i]    if (width - temp[1]) % 2:        if cc % 2 == 0:            temp[0] += ' '            temp[1] += 1            cc += 1        else :            temp[0] = ' ' + temp[0]            temp[1] += 1            cc += 1    sw = (width - temp[1]) / 2    lw = '*' + ' '*(sw-1)    rw = ' '*(sw-1) + '*'    temp[0] = lw + temp[0] + rw    print temp[0]print '*' * width



0 0
原创粉丝点击