华为4/22笔试题AK代码
这次笔试感觉还好,一个小时AK。
第一题和第二题都是对字符的处理,第二题刚开始只A了60%几,后来发现是没有判断代表个数的那位是否可强制转换(后来默默用了try except😂)。
第三题贪心,首先求出所有数和的1/k,遍历数组并求和,设为s,当s大于所有数和的1/k时,此数之前的数为一个分割区域,初始化s为此数,同时k-1,不能满足所有情况,只能恰好过所有的测试用例。
第一题:
s = input() dic = {'0','1','2','3','4','5','6','7','8','9'} data = [] for i in s: if i in dic: data.append(int(i)) data.sort() data = [str(i) for i in data] print(''.join(data))
第二题:
data = input().split() ind_5a = [] for i in range(len(data)): if data[i] == '5a': ind_5a.append(i) if not ind_5a&nbs***bsp;len(ind_5a)==1: print('') else: dataList = [] for i in range(len(ind_5a)-1): dataList.append(data[ind_5a[i]:ind_5a[i+1]]) dataL2 = [] for i in dataList: if i: i.pop(0) dataL2.append(i) dataList = dataL2 res = [] for i in dataList: num = 0 for j in range(len(i)-1): if i[j] == '5b': continue else: num += 1 try: if num == int(i[-1]): res.append(i) except: pass r = '5a ' for i in res: r += ' '.join(i) r += ' 5a ' print(r[:-1])第三题:
m, k = list(map(int,input().split())) data = input().split() dataInt = list(map(int,data)) if m == k: print(' / '.join(data)) else: sumData = sum(dataInt)/k res = [] s = 0 for i in range(m): s = s + dataInt[i] if s > sumData and k>1: k -= 1 res.append('/') res.append(data[i]) s = dataInt[i] else: res.append(data[i]) print(' '.join(res))