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

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值