携程 算法岗 笔试 记录贴
8.30 笔试 10到单选题,4道编程题
1. 对输入的每个数字变换为偶数,如果不能输出-1,多解输出任意一个即可(注意数的第一位不能为0)ac
n = int(input()) for i in range(n): num = input() result = '' tag = 0 if int(num[-1]) % 2 == 0: print(num) elif int(num[0]) % 2 == 0: for j in range(1,len(num)): if int(num[j]) != 0: result += num[j:] result += num[:j] break print(result) else: for j in range(len(num)): if int(num[j]) % 2 == 0: tag =1 tmp = num[j] result += num[j+1:] result += num[j] print(result) break else: result += num[j] if tag == 0: print(-1)2. 有a个‘y’,b个‘o’,c个‘u’组成字符串,连续的‘you’得2分,连续‘oo’得1分,输出最大得分 ac
n = int(input()) for i in range(n): a,b,c = map(int,input().strip().split()) min_num = min(a,b,c) if b > min_num: result = 2 * min_num + (b-min(a,b,c) - 1) else: result = 2 * min_num print(result)3.游游的三色树。
没思路,输出1过了6%。想讨论一下
4. 一个数组,定义滑动窗口值为数组中相邻两个数之差的最大值,可以修改数组的一个数或者不修改,求最小滑动窗口值。只ac了94% 不知道哪种情况没考虑到
import math n = int(input()) nums = list(map(int,input().strip().split())) nums_diff = [] for i in range(1,len(nums)): nums_diff.append(nums[i] - nums[i-1]) max_diff = max(nums_diff) min_diff = min(nums_diff) tag = 0 for i in range(len(nums_diff)): if nums_diff[0] == max_diff: tag += 1 nums_diff[0] = 0 elif nums_diff[-1] == max_diff: nums_diff[-1] = 0 tag += 1 if nums_diff[i] == max_diff: tag += 1 if nums_diff[i-1] < nums_diff[i+1]: nums_diff[i] = math.ceil((nums_diff[i] + nums_diff[i-1]) / 2) else: nums_diff[i] = math.ceil((nums_diff[i] + nums_diff[i+1]) / 2) if tag == 1: print(max(nums_diff)) else: print(max_diff)