百度2016笔试(算法春招实习)

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)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值