2016百度春季笔试

来源:互联网 发布:软件测试原则包括 编辑:程序博客网 时间:2024/03/29 20:33







4.23 10:00更新,编程题1的Python实现,仅供参考。源码见页尾

4.23 20:35更新,编程题2的Python实现。源码见尾页

百度的题还是非常偏重算法的,整体来讲难度比较高,尤其是编程题,下面附上原题:


选择题


问答题


主观题


编程题


编程题1源码

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #coding:utf-8  
  2.   
  3. data = []  
  4.   
  5. # 处理输入  
  6. while True:  
  7.     item = []  
  8.     theString = ''  
  9.     theString = raw_input() # theString = '(??)'  
  10.     if len(theString) == 0:  
  11.         break  
  12.     else:  
  13.         item.append(theString)  
  14.         for i in range(theString.count('?')):  
  15.             aibi = raw_input() # '1 2'  
  16.             ai = int(aibi.split(' ')[0])  
  17.             bi = int(aibi.split(' ')[1])  
  18.             aibi = []  
  19.             aibi.append(ai); aibi.append(bi) # aibi = [1,2]  
  20.             item.append(aibi) # item = [['(??)'], [1,2], [2,8]]  
  21.     data.append(item) #data = [  ['(??)', [1,2], [2,8]], ......  ]  
  22.   
  23. # 生成所有括号的可能性  
  24. def allThePosibility(theString,data):  
  25.     # 对于该问号有改成)和(这两种可能  
  26.     if theString.count('?') == 0:  
  27.         data.append(theString)  
  28.     else:  
  29.         theStringToLeft = ''  
  30.         theStringToRight = ''  
  31.         theIndex = theString.index('?'# 第一个问号的位置  
  32.         theStringToLeft = theString[:theIndex] + '(' + theString[theIndex+1:]  
  33.         #print theStringToLeft  
  34.         theStringToRight = theString[:theIndex] + ')' + theString[theIndex + 1:]  
  35.         #print theStringToRight  
  36.         allThePosibility(theStringToLeft,data)  
  37.         allThePosibility(theStringToRight,data)  
  38.         return data # ['((()', '(())', '()()', '()))']  
  39.   
  40. # 是否正则化  
  41. def isRegularization(theString): # theString = '((()'  
  42.     if theString.count('(') != theString.count(')'):  
  43.         return 0  
  44.     stack = []  # 设置一个栈  
  45.     for alphabet in theString:  
  46.         if alphabet == ')' and stack == []:  
  47.             return 0  
  48.         else:  
  49.             if alphabet == '(':  
  50.                 stack.append(0# 入栈  
  51.             else# 遇到右括号  
  52.                 stack.pop() # 出栈  
  53.     if stack != []:  
  54.         return 0  
  55.     else:  
  56.         return theString  
  57.   
  58. # 每个问号的位置  
  59. def positionOfQuestionMark(theString): # theString = '(??)'  
  60.     i = 0  
  61.     position = []  
  62.     while True:  
  63.         if '?' in theString[i:]:  
  64.             theIndex = theString[i:].index('?'# 更新下一个问号的位置  
  65.             i += theIndex  
  66.             position.append(i)  
  67.             i += 1  
  68.         else:  
  69.             break  
  70.     return position # [1,2]  
  71.   
  72. # 处理数据  
  73. for item in data: # item = ['(??)', [1,2], [2,8]]  
  74.   
  75.     regularzations = []  
  76.   
  77.     # 所有括号的位置  
  78.     position = positionOfQuestionMark(item[0]) # position = [1,2]  
  79.     # 列出所有能加括号的情况  
  80.     posibilities = allThePosibility(item[0], data=[]) # posibilities = ['((()', '(())', '()()', '()))']  
  81.     # 所有能正则化的情况  
  82.     for theString in posibilities:  
  83.         if isRegularization(theString) != 0:  
  84.             regularzations.append(theString) # regularzations = ['(())', '()()']  
  85.     if regularzations == []: # 没有正则化  
  86.         print -1  
  87.         break  
  88.   
  89.     # 计算最小代价  
  90.     minValue = 9999  
  91.     minValueReg = ''  
  92.     for reg in regularzations: # reg = '(())'  
  93.         value = 0  
  94.         flag = 1  
  95.         for i in position:  
  96.             if reg[i] == '(':  
  97.                 value += item[flag][0# 加上左括号的代价  
  98.             else# ')'  
  99.                 value += item[flag][1# 加上右括号的代价  
  100.             flag += 1  
  101.         if value < minValue:  
  102.             minValue = value  
  103.             minValueReg = reg  
  104.     print minValue  
  105.     print minValueReg  



编程题2

[python] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #coding:utf-8  
  2.   
  3. # 百度笔试题2  
  4.   
  5. # 先处理输入  
  6. theString = raw_input()  
  7. base = int(theString.split(' ')[0]) # string型  
  8. luckyNum = int(theString.split(' ')[1]) # string型  
  9.   
  10. if base < luckyNum:  
  11.     print luckyNum  
  12. elif base > luckyNum and len(str(base)) > len(str(luckyNum)):  
  13.     x = len(str(luckyNum)) # 幸运数的位数  
  14.     if int(str(base)[len(str(base)) - x : ]) <= luckyNum: # 如果base的后x位小于等于幸运数  
  15.         print str(base)[:len(str(base)) - x] + str(luckyNum)  
  16.     else# base的后x为大于幸运数  
  17.         tagNum = int(str(base)[len(str(base)) -x -1:len(str(base)) -x]) # base比x高一位的数,int型  
  18.         tagNum += 1  
  19.         answer = (str(base)[:len(str(base)) - x - 1]) + str(tagNum) + str(luckyNum)  
  20.         print answer  
  21. elif base > luckyNum and len(str(base)) == len(str(luckyNum)):  
  22.     # 在luckNum的左面写个1就行了  
  23.     print '1' + str(luckyNum)  











0 0