1. 程式人生 > >百度2016筆試(算法春招實習)

百度2016筆試(算法春招實習)

src mar ole 源代碼 .... += 生成 選擇題 name

4.23 10:00更新。編程題1的Python實現。僅供參考。源代碼見頁尾

4.23 20:35更新,編程題2的Python實現。源代碼見尾頁

百度的題還是很偏重算法的。總體來講難度比較高。尤其是編程題,以下附上原題:


選擇題

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享


問答題

技術分享

技術分享


主觀題

技術分享


編程題

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享


編程題1源代碼

#coding:utf-8

data = []

# 處理輸入
while True:
    item = []
    theString = ''
    theString = raw_input() # theString = '(??)'
    if len(theString) == 0:
        break
    else:
        item.append(theString)
        for i in range(theString.count('?')):
            aibi = raw_input() # '1 2'
            ai = int(aibi.split(' ')[0])
            bi = int(aibi.split(' ')[1])
            aibi = []
            aibi.append(ai); aibi.append(bi) # aibi = [1,2]
            item.append(aibi) # item = [['(?

?)'], [1,2], [2,8]] data.append(item) #data = [ ['(?

?)', [1,2], [2,8]], ...... ] # 生成全部括號的可能性 def allThePosibility(theString,data): # 對於該問號有改成)和(這兩種可能 if theString.count('?

') == 0: data.append(theString) else: theStringToLeft = '' theStringToRight = '' theIndex = theString.index('?') # 第一個問號的位置 theStringToLeft = theString[:theIndex] + '(' + theString[theIndex+1:] #print theStringToLeft theStringToRight = theString[:theIndex] + ')' + theString[theIndex + 1:] #print theStringToRight allThePosibility(theStringToLeft,data) allThePosibility(theStringToRight,data) return data # ['((()', '(())', '()()', '()))'] # 是否正則化 def isRegularization(theString): # theString = '((()' if theString.count('(') != theString.count(')'): return 0 stack = [] # 設置一個棧 for alphabet in theString: if alphabet == ')' and stack == []: return 0 else: if alphabet == '(': stack.append(0) # 入棧 else: # 遇到右括號 stack.pop() # 出棧 if stack != []: return 0 else: return theString # 每一個問號的位置 def positionOfQuestionMark(theString): # theString = '(?

?

)' i = 0 position = [] while True: if '?

' in theString[i:]: theIndex = theString[i:].index('?

') # 更新下一個問號的位置 i += theIndex position.append(i) i += 1 else: break return position # [1,2] # 處理數據 for item in data: # item = ['(??)', [1,2], [2,8]] regularzations = [] # 全部括號的位置 position = positionOfQuestionMark(item[0]) # position = [1,2] # 列出全部能加括號的情況 posibilities = allThePosibility(item[0], data=[]) # posibilities = ['((()', '(())', '()()', '()))'] # 全部能正則化的情況 for theString in posibilities: if isRegularization(theString) != 0: regularzations.append(theString) # regularzations = ['(())', '()()'] if regularzations == []: # 沒有正則化 print -1 break # 計算最小代價 minValue = 9999 minValueReg = '' for reg in regularzations: # reg = '(())' value = 0 flag = 1 for i in position: if reg[i] == '(': value += item[flag][0] # 加上左括號的代價 else: # ')' value += item[flag][1] # 加上右括號的代價 flag += 1 if value < minValue: minValue = value minValueReg = reg print minValue print minValueReg




編程題2

#coding:utf-8

# 百度筆試題2

# 先處理輸入
theString = raw_input()
base = int(theString.split(' ')[0]) # string型
luckyNum = int(theString.split(' ')[1]) # string型

if base < luckyNum:
    print luckyNum
elif base > luckyNum and len(str(base)) > len(str(luckyNum)):
    x = len(str(luckyNum)) # 幸運數的位數
    if int(str(base)[len(str(base)) - x : ]) <= luckyNum: # 假設base的後x位小於等於幸運數
        print str(base)[:len(str(base)) - x] + str(luckyNum)
    else: # base的後x為大於幸運數
        tagNum = int(str(base)[len(str(base)) -x -1:len(str(base)) -x]) # base比x高一位的數,int型
        tagNum += 1
        answer = (str(base)[:len(str(base)) - x - 1]) + str(tagNum) + str(luckyNum)
        print answer
elif base > luckyNum and len(str(base)) == len(str(luckyNum)):
    # 在luckNum的左面寫個1即可了
    print '1' + str(luckyNum)


百度2016筆試(算法春招實習)