2020 华为机试 三道编程题

投的网络安全岗位,三道编程题。其实不难....

测试用例都过了。但是....

第一题,不太熟悉newcode输入输出,python用 for line in sys.stdin 读一行数据有问题,导致花了半个小时调试。

第二题由于多了一个print导致0通过率。

第三题由于时间复杂度问题通过20%。

这里贴一下渣代码吧,不好意思贴原来博客上了。

#coding=utf-8
# 本题为考试单行多行输入输出规范示例,无需提交,不计分。
import sys

def replace(string, word):
    """
    replace the word in the string
    """
    if word in string:
        replaceWord = len(word)*"*"
        result = string.replace(word, replaceWord)
    else:
        result = string
    return result


if __name__ == "__main__":
    while True:
        input_line = sys.stdin.readline().replace('\n', '')
        # print input_line
        if not input_line:
            break;
        string = input_line.split(" ")[0]
        word = input_line.split(" ")[1]
        # print string
        # print word
        result = 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 3

3
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 integer

	for i in xrange(operateNum):
		operate = sys.stdin.readline().replace('\n', '').split(' ')
		if operate[0] == 'Q':       # Q
			offLeft = int(operate[1]) - 1
			offRight = int(operate[2]) - 1
			number = offRight - offLeft + 1
			# print offLeft, offRight
			tmpOperate = data[offLeft: offRight + 1]
			# print tmpOperate
			sum = 0
			for i in tmpOperate:
				sum += int(i)
			result = sum/number
			print result

		elif 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 3


1 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 daysNumber
		data = sys.stdin.readline().replace('\n', '').split(' ')
		maxScore = 0
		allScore = 0
		for i in xrange(len(data)):
			todayScore = 0
			if i == 0:                  # first day
				continue
			for j in range(i):
				if data[j] > data[i] :
					todayScore -= 1;
				elif data[j] < data[i] :
					todayScore += 1;

			allScore += todayScore

			if allScore > maxScore:
				maxScore = allScore

		print maxScore, allScore
发布了242 篇原创文章 · 获赞 95 · 访问量 61万+

猜你喜欢

转载自blog.csdn.net/think_ycx/article/details/101387753
今日推荐