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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章