本文主要是介绍2020 华为机试 三道编程题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
投的网络安全岗位,三道编程题。其实不难....
测试用例都过了。但是....
第一题,不太熟悉newcode输入输出,python用 for line in sys.stdin 读一行数据有问题,导致花了半个小时调试。
第二题由于多了一个print导致0通过率。
第三题由于时间复杂度问题通过20%。
#coding=utf-8
# 本题为考试单行多行输入输出规范示例,无需提交,不计分。
import sysdef replace(string, word):"""replace the word in the string"""if word in string:replaceWord = len(word)*"*"result = string.replace(word, replaceWord)else:result = stringreturn resultif __name__ == "__main__":while True:input_line = sys.stdin.readline().replace('\n', '')# print input_lineif not input_line:break;string = input_line.split(" ")[0]word = input_line.split(" ")[1]# print string# print wordresult = replace(string, word)print result
第二题:
#coding=utf-8
# 本题为考试单行多行输入输出规范示例,无需提交,不计分。
import sys
'''
6
8
1 2 3 4 5 6
Q 1 6
U 2 6
U 4 3
Q 2 4
Q 1 2
U 1 3
U 2 1
Q 1 33
6
4
5
'''
if __name__ == "__main__":areaNum = int(sys.stdin.readline().replace('\n', ''))operateNum = int(sys.stdin.readline().replace('\n', ''))data_input = sys.stdin.readline().replace('\n', '').split(' ')data = [int(x) for x in data_input] # to integerfor i in xrange(operateNum):operate = sys.stdin.readline().replace('\n', '').split(' ')if operate[0] == 'Q': # QoffLeft = int(operate[1]) - 1offRight = int(operate[2]) - 1number = offRight - offLeft + 1# print offLeft, offRighttmpOperate = data[offLeft: offRight + 1]# print tmpOperatesum = 0for i in tmpOperate:sum += int(i)result = sum/numberprint resultelif operate[0] == 'U':# print 'U' # U# operate = sys.stdin.readline().replace('\n', '').split(' ')id = int(operate[1]) - 1# print 'old', data[id]data[id] += int(operate[2])# print 'new', data[id]
第三题:
#coding=utf-8
# 本题为考试单行多行输入输出规范示例,无需提交,不计分。
import sys
'''
2
3
1 3 2
3
2 1 31 1
1 1
'''
if __name__ == "__main__":number = int(sys.stdin.readline().replace('\n', ''))for i in xrange(number):daysNumber = int(sys.stdin.readline().replace('\n', ''))# print daysNumberdata = sys.stdin.readline().replace('\n', '').split(' ')maxScore = 0allScore = 0for i in xrange(len(data)):todayScore = 0if i == 0: # first daycontinuefor j in range(i):if data[j] > data[i] :todayScore -= 1;elif data[j] < data[i] :todayScore += 1;allScore += todayScoreif allScore > maxScore:maxScore = allScoreprint maxScore, allScore
这篇关于2020 华为机试 三道编程题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!