# 站队

<题目来源: 京东2017秋招 原题链接-可在线提交(赛码网) >

### 问题描述

......------(1)------..............---(2)---........................
---------------------------(3)---------------------------

``````import sys

def main():

segments = []
for i, ch in enumerate(line):
if '1' <= ch <= '9':
t = int(ch) - int('0')
left = max(0, i - t)
right = min(n - 1, i + t)
segments.append((left, right))

segments = sorted(segments, key=lambda s: s[0])
#print segments

current = -1
res = 0
for segment in segments:
if segment[0] <= current:
l = current + 1
r = segment[1]
else:
l = segment[0]
r = segment[1]

for i in range(l, r + 1):
if line[i] == 'X':
res += 1

if r > current:
current = r

print res

if __name__ == '__main__':
main()
``````

# 第K个幸运数

<题目来源: 京东2017实习生招聘 原题链接-可在线提交(赛码网) >

### 问题描述

4和7是两个幸运数字，我们定义，十进制表示中，每一位只有4和7两个数的正整数都是幸运数字。前几个幸运数字为：4,7,44,47,74,77,444,447...

___4
___7
__44
__47
__74
__77
_444
_447
_474
_477
_744
_747
_774
_777
4444
...

``````import sys

def main():
for c in range(case):

sum2s = 1
num = []
while True:
sum2s *= 2
if n - sum2s + 2 > 0:
r = (n - sum2s + 2) % sum2s
if 1 <= r <= sum2s / 2:
num.insert(0, '4')
else:
num.insert(0, '7')
else:
break

print ''.join(num)

if __name__ == "__main__":
main()
``````

# 选举游戏

<题目来源: 京东2016实习生招聘 原题链接-可在线提交(赛码网) >

### 问题描述

``````import sys
import heapq

def main():
while True:
if len(line) < 0:
break

n = line[0]
heap = []

for i, l in enumerate(line):
if i:
heapq.heappush(heap, -l)

cnt = 0
while True:
e = -heapq.nsmallest(1, heap)[0]
if line[0] > e:
break
line[0] += 1
cnt += 1
e -= 1
heapq.heapreplace(heap, -e)

print cnt

if __name__ == '__main__':
main()
``````

# 交易清单

<题目来源: 京东2016实习生招聘 原题链接-可在线提交(赛码网) >

### 问题描述

``````import sys

def main():
while True:
if len(line) < 2:
break

n, s = line[0], line[1]

dict_b = {}
dict_s = {}
for i in range(n):

if line[0] == 'B':
r = dict_b.get(line[1])
if r is None:
dict_b[line[1]] = int(line[2])
else:
dict_b[line[1]] = r + int(line[2])
else:
r = dict_s.get(line[1])
if r is None:
dict_s[line[1]] = int(line[2])
else:
dict_s[line[1]] = r + int(line[2])

list_b = []
list_s = []
for k, v in dict_b.iteritems():
list_b.append((int(k), v))
list_b = sorted(list_b, reverse=True)

for k, v in dict_s.iteritems():
list_s.append((int(k), v))
list_s = sorted(list_s)

p = min(s, len(list_s))
for e in list_s[p::-1]:
print 'S', e[0], e[1]

p = min(s, len(list_b))
for e in list_b[:p]:
print 'B', e[0], e[1]

if __name__ == '__main__':
main()
``````

# 进制均值

<题目来源: 京东2017秋招 原题链接-可在线提交(赛码网) >

### 问题描述

``````import sys

def gcd(a, b):
c = a % b
if c == 0:
return b

return gcd(b, c)

def main():
while True:

rest_sum = 0
for i in range(2, n):
temp = n
while temp:
rest_sum += temp % i
temp /= i

g = gcd(rest_sum, n - 2)

print("%d/%d" % (rest_sum / g, (n - 2) / g))

if __name__ == '__main__':
main()
``````

1 人关注
12 篇文章