快手 算法笔试题目大题2019-3-30
第一题
选择一个数里面出现的奇次数和偶次数的个数
num = int(input()) list1 = [int(tmp) for tmp in input().strip().split(" ")] jishu = [] oshu = [] for i in range(0,len(list1)): if list1[i] in jishu: oshu.append(list1[i]) jishu.remove(list1[i]) elif list1[i] in oshu: jishu.append(list1[i]) oshu.remove(list1[i]) else: jishu.append(list1[i]) for i in jishu: print(i)
判断是否是递增的序列 或者 修改一个数 让他是递增的序列
list1 = [int(tmp) for tmp in input().strip().split(" ")] def check(list1): for i in range(len(list1)-1): if list1[i] > list1[i+1]: return -1 return 1 def checklist(list1): for i in range(len(list1) - 1): if list1[i] > list1[i + 1]: if (i == 0): list1[i] = list1[i + 1] else: list1[i] = list1[i - 1] res = check(list1) if (res == -1): print(0) else: print(1) break count = 0 for i in range(len(list1)-1): if list1[i] < list1[i+1]: count = count+1 if count == len(list1)-1: print(1) else: checklist(list1)
第三题
实现一个计算器功能 使得 + -* () 满足
python
import re import sys def format_input(input_re_value): input_re_value = input_re_value.replace(" ", "") #去掉输入的空格 input_re_value = re.split(r'(\D)', input_re_value) while True: if "" in input_re_value: input_re_value.remove("") #将列表中""去掉 else: break return input_re_value # 计算没有括号的列表的值。加减乘除 def comput(re_value): while "*" in re_value or "/" in re_value: for i, j in enumerate(re_value): if j == "*": re_cheng = int(re_value[i - 1]) * int(re_value[i + 1]) #re_cheng = re_value[i - 1] * re_value[i + 1] re_value.pop(i - 1) re_value.pop(i - 1) re_value.pop(i - 1) re_value.insert(i - 1, re_cheng) break if j == "/": re_chu = int(re_value[i - 1]) / int(re_value[i + 1]) re_chu = int(re_chu) re_value.pop(i - 1) re_value.pop(i - 1) re_value.pop(i - 1) re_value.insert(i - 1, re_chu) break while "+" in re_value or "-" in re_value: for i, j in enumerate(re_value): if j == "+": re_jia = int(re_value[i - 1]) + int(re_value[i + 1]) re_value.pop(i - 1) re_value.pop(i - 1) re_value.pop(i - 1) re_value.insert(i - 1, re_jia) break if j == "-": re_jian = int(re_value[i - 1]) - int(re_value[i + 1]) re_value.pop(i - 1) re_value.pop(i - 1) re_value.pop(i - 1) re_value.insert(i - 1, re_jian) break return re_value[0] def bracket(input_str): if not re.search(r'\(([^()]+)\)', input_str): #若没有括号 s = format_input(input_str) return comput(s) else: #有括号 li = re.split(r'\(([^()]+)\)', input_str) data = re.search(r'\(([^()]+)\)', input_str).group() data_strip = data.strip("()") inde = li.index(data_strip) #取得其下标 ret = comput(format_input(data_strip)) #参数为列表形式 li.pop(inde) li.insert(inde, str(ret)) re_str = "".join(li) if "+" not in re_str and "-" not in re_str and "*" not in re_str \ and "/" not in re_str and "(" not in re_str and ")" not in re_str: return re_str #为什么没有return else: return bracket(re_str) #迭代 input = input() res = bracket(input) print(res)c语言
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x, y;
char input;
int ret = 0;
scanf("%d%c%d", &x,&input,&y);
switch (input)
{
case'+':
ret = x + y;
break;
case'-':
ret = x - y;
break;
case'*':
ret =x * y;
break;
default:
break;
}
printf("%d\n", ret);
return 0;
}
#快手##笔试题目##题解##笔经##include <stdlib.h>
int main()
{
int x, y;
char input;
int ret = 0;
scanf("%d%c%d", &x,&input,&y);
switch (input)
{
case'+':
ret = x + y;
break;
case'-':
ret = x - y;
break;
case'*':
ret =x * y;
break;
default:
break;
}
printf("%d\n", ret);
return 0;
}