唉不知道能不能过,求大佬给debug指导和测试用例
4道编程题实现出1,2题
第3题(忘了复制代码)牛客网python不知道为什么吞掉一个bug没任何报错,调试浪费太多时间凉凉
#9进制数字相加,通过75% def add(num1 , num2): # write code here hasDot = False if '.' in num1&nbs***bsp;'.' in num2: hasDot = True n1 = num1.split('.') n2 = num2.split('.') if len(n1[0]) > len(n1[0]): n2[0] = (len(n1[0]) - len(n2[0])) * '0' + n2[0] else: n1[0] = (len(n2[0]) - len(n1[0])) * '0' + n1[0] if len(n1) == 1: n1.append('') if len(n2) == 1: n2.append('') if len(n1[1]) > len(n2[1]): n2[1] = n2[1] + (len(n1[1]) - len(n2[1])) * '0' else: n1[1] = n1[1] + (len(n2[1]) - len(n1[1])) * '0' num1 = n1[0] + n1[1] num2 = n2[0] + n2[1] dec = max(len(n1[1]), len(n2[1])) res = '' carry = 0 for i in range(len(num1) - 1, -1, -1): tmp = int(num1[i]) + int(num2[i]) + carry if tmp >= 9: carry = 1 tmp -= 9 else: carry = 0 res = str(tmp) + res if carry: res = str(carry) + res #print(res) if hasDot: res = res[:-dec] + '.' + res[-dec:] #print(res) return res add("123.25","44.15")
#工人分配方案数量,通过大概45% #用memoization,但是怎么去掉重复方案啊? import sys inputs = [] for l in sys.stdin: inputs.append(l) ws = inputs[1].strip().split(' ') js = inputs[2].strip().split(' ') nums = {} def memo(ws, js): #memoize if tuple(ws) in nums: return nums[tuple(ws)] if len(ws) == 1: if ws[0] >= js[0]: return 1 else: return 0 #duplicate numbers -> factorial first = ws[0] dup = True for w in ws: if not w == first: dup = False break if dup: fac = 1 for i in range(1, len(ws) + 1): fac *= i #memoize nums[tuple(ws)] = fac return fac total = 0 #rotate for i in range(len(ws)): for j in range(1, len(ws)): l = memo(ws[:j],js[:j]) r = memo(ws[j:],js[j:]) total = max(total, l * r) #print(ws, js) #print(l, r, total) ws.append(ws.pop(0)) #memoize nums[tuple(ws)] = total #print(ws, total) return total print(memo(ws, js) % int(inputs[3]))
全部评论
(6) 回帖