您的位置:首页 > 其它

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

2017-08-13 08:09 435 查看
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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: