2018科大讯飞在线笔试题(编程题部分)

来源:互联网 发布:上海泛微网络 编辑:程序博客网 时间:2024/06/05 17:05
1.有n个人排成了一行队列,每个人都有一个站立的方向:面向左或面向右。由于这n个人中每个人都很讨厌其他的人,
所以当两个人面对面站立时,他们会发生争吵,然后其中一个人就会被踢出队列,谁被踢出队列都是有可能的。我
们用字符 L 来表示一个面向左站立的人,用字符 R 来表示一个面向右站立的人,那么这个队列可以用一个字符串
描述。比如 RLLR 就表示一个四个人的队列,其中第一个人和第二个人是面对面站立的。他们发生争吵后队列可能
会变成 LLR,也可能变成 RLR;若变成 RLR,则第一个人与第二个人还会发生争吵,队列会进一步变成 LR 或者RR。

若在某个时刻同时可能有很多的争吵会发生时,接下来只会发生其中的一个,且任意一个都是有可能发生的。你想
知道经过一系列的争吵后,这个队列最少会剩下多少人?


输入
第一行包含一个有字符 L 和 R 构成的字符串。1 ≤字符串长度≤ 105


输出
输出队列中最少会剩下多少人。


样例输入
LRRLRL


样例输出

2

我的答案:

import sysdef getMiniNums():    global globalPeopleList    if not globalPeopleList:        return 0    l = globalPeopleList[0]    min = len(l)        for l in globalPeopleList:        if min > len(l):            min = len(l)            return mindef copyList(temp , input_list):    for i in range(0,len(input_list)) :    temp.insert(i,input_list[i])globalPeopleList = []def  getRemainsPeople(input_list):    #print (input_list)     if len(input_list) < 2:        return []    # print (input_list)    for i in range(0,len(input_list)-1):        if input_list[i] == 'R' and input_list[i+1] == 'L':            input_list2 = []            input_list3 = []            copyList(input_list2 , input_list)            copyList(input_list3 , input_list)            del input_list3[i+1]            del input_list2[i]            if not input_list2 or not input_list3:                break                 if len(input_list) < 2:                break            getRemainsPeople(input_list2)            getRemainsPeople(input_list3)    # print (input_list)    globalPeopleList.append(input_list)    return input_list    if __name__ == '__main__':     input_str = sys.stdin.readline().strip()    getRemainsPeople(list(input_str))    print(getMiniNums())
2.大学生举办足球赛,输入测试数据有多组,每组测试数据的第一行为一个整数n(1=< n <=50),为参与总决赛的球队数,
随后的n行为球队的名字,由不超过30个的大小写拉丁字母构成。随后的n*(n-1)/2行为赛事的开展情况,每行的格式为
name1-name2 num1:num2,表示两支队伍的比分情况(1=<num1, num2<=100)。确保不会有两支队伍同名,也不会出现队伍自
己通自己比赛的情况,且每场比赛仅出现一次。
输出
对每组测试数据,输出n/2行,为按字母序排列的进入淘汰赛的n/2支队伍的名单,每个名字在单独的行中输出。

样例输入
4
A
B
C
D
A-B 1:1
A-C 2:2
A-D 1:0
B-C 1:0
B-D 0:3
C-D 0:3
2
a
A
a-A 2:1
样例输出
A
D
a
我的答案:

import sysimport operatordef  getScoreList(teamNamesCollection,matchCollection):    for i in range(0,len(teamNamesCollection)):        teamNames = teamNamesCollection[i]        matchList = matchCollection[i]        ScoreStatistics = {}        teamOutput = []        scoreList = []        for name in teamNames:            ScoreStatistics [name] = 0        for match in  matchList:            teamName1,teamName2 = match[0],match[1]            score1,score2 = int(match[2]),int(match[3])            if teamName1 in teamNames and teamName2 in teamNames:                ScoreStatistics [teamName1] += score1                ScoreStatistics [teamName2] += score2        # sorted(ScoreStatistics.items(),key=operator.itemgetter(1))#values sort        # f = filter(lambda i: i[0] != "b" , a.items())          # teamOutput.append(ScoreStatistics.key)        for k,v in ScoreStatistics.items():            scoreList.append(v)        scoreList = sorted(scoreList)        n = int(len(scoreList) /2)        scoreList = scoreList[n:]        # print (ScoreStatistics)        # print (scoreList)        for s in scoreList:            ls = list(ScoreStatistics.keys())[list(ScoreStatistics.values()).index(s)]            print (''.join(ls))                             if  __name__ == '__main__':    matchCollection = []    teamNamesCollection =  []     k = 0    while True:        matchList = []        input_num = sys.stdin.readline().strip()        if input_num == "":            break        input_num = int(input_num)        i = 0        teamNames = []                while i < input_num:            teamName = sys.stdin.readline().strip()            teamNames.insert(i,teamName)             i += 1        i = 0         matchCounts = int(input_num*(input_num-1)/2)        while i < matchCounts:            matchItemList = []            matchItem = sys.stdin.readline().strip()                    teams,names = matchItem.split(' ')            teamList = teams.split('-')            nameList = names.split(':')                        matchItemList.extend(teamList)            matchItemList.extend(nameList)            matchList.insert(i,matchItemList)            i += 1        matchCollection.insert(k,matchList)        teamNamesCollection.insert(k,teamNames)        k += 1    # print (matchList)    getScoreList(teamNamesCollection,matchCollection)