您的位置:首页 > 其它

2019春招笔试凉经-字节跳动20190316

2019-03-16 19:42 302 查看

 

1.求找钱最少给几个硬币

有1024元钱 输入你花掉的数目问找回的硬币数最小

有64 16 4 1 的硬币

[code]res=1024-n
cnt=0
while res>=64:
res-=64
cnt+=1
while res>=16:
res-=16
cnt+=1
while res>=4:
res-=4
cnt+=1
while res>=1:
res-=1
cnt+=1
print(cnt)

2. 王大锤

在字符串中有连续的三个字母要去掉一个

有连续的AABB型的  要去掉第二对中的一个字母 即AAB

匹配顺序是从左到右例如AABBCC 结果为AABCC。

 

# 先遍历一遍,去掉连续三个及以上的连续相同字母,如AAAABBBCC变为AABBCC.

# 再遍历一遍,如果有AABB则变为AAB。

[code]
s=['helloo', 'woooooow','wwbbddggoxsanovoo']
for str1 in s:
tmp=''
for i in range(len(str1)):
if i<len(str1)-2:
if str1[i]==str1[i+1] and str1[i+1]==str1[i+2]:
pass
else:
tmp+=str1[i]
else:
tmp+=str1[i]
print(tmp)
i=0
while i<len(tmp)-3:
if tmp[i]==tmp[i+1] and tmp[i+2]==tmp[i+3]:
tmp=tmp[:i+3]+tmp[i+4:]
i+=3
else:
i+=1
print(tmp)

 

 

 

3.发奖品

 

###  还没跑出来。。等等填坑

4.  剪绳子

暴力二分

[code]N=3
M=4
L=[3,5,4]
def isok(L, M, target):  # 输入数组L,可否分成target条长为M的绳子
cnt=0
for l in L:
cnt+=l//target
if cnt>=M:
return True
else:
return False

left=0
right=1e9
eps=1e-5   # 如果区间几乎不再收缩了,就可以跳出while
while left<right-eps:
mid=(right+left)/2.0
if isok(L,M,mid):
left=mid
else:
right=mid
print('%3.2f'%round(left,2))  # 四舍五入 输出两位小数

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: