bilibili, 算法第二次笔试
第一题:
if __name__ == "__main__": d = list(map(int, input().split(','))) for i in range(len(d)): for j in range(i): if int(str(d[i]) + str(d[j])) > int(str(d[j]) + str(d[i])): d[i], d[j] = d[j], d[i] d = [str(i) for i in d] if len(set(d)) == 1 and set(d).pop() == '0': print("0") else: print("".join(d))
class int_com: def __init__(self, val): self.val = val def __lt__(self, other): str1 = str(self.val) + str(other.val) str2 = str(other.val) + str(self.val) num1 = int(str1) num2 = int(str2) return num1 < num2 def __eq__(self, other): str1 = str(self.val) + str(other.val) str2 = str(other.val) + str(self.val) num1 = int(str1) num2 = int(str2) return num1 == num2 if __name__ == "__main__": num = [int(x) for x in input().strip().split(',')] nums = [] for i in range(len(num)): t = int_com(num[i]) nums.append(t) nums.sort(reverse=True) res = [str(x.val) for x in nums] print(res) print(''.join(res))
第三题:
if __name__ == "__main__": n, s = [int(x) for x in input().strip().split(' ')] p = [int(x) for x in input().strip().split(' ')] if sum(p) <= s: print(-1) else: res = float('inf') cnt = p[0] l = 0 r = 1 while l < n and r <= n: while r < n and cnt < s: cnt += p[r] r += 1 if cnt >= s: res = min(res, r-l) cnt -= p[l] l += 1 print(res)