搜狐畅游笔试题
一组无序的自然数集合,由0,1,2… …,n的数字和一个的数字X(X>=0 && X<=n)组成,请从集合中找出这个重复数字X。
输入描述:
空格分割的自然数集合
输出描述:
重复数字
输入例子1:
9 6 4 2 1 3 5 7 0 1 8
输出例子1:
1
例子说明1:
0,1,2,3,4,5,6,7,8,9 + 1
Python实现,利用字符串内建函数string.count(str, beg=0, end=len(string))
s = str(input()).split(" ")
s1 =list(set(s))
di = dict().fromkeys(s1, 0)
for char in s:
di[char] = s.count(char)
for key in di:
if di[key] ==2:
print(key)
给定无序整数序列,求连续非空子串最大和,例如{-23 17 -7 11 -2 1 -34},子串为{17,-7,11},最大和为21
arr = list(map(int, input().split()))
temp = arr[0]
max1 = arr[0]
for i in range(1,len(arr)):
temp = temp + arr[i]
if arr[i] > temp:
temp = arr[i]
if temp > max1:
max1 = temp
print(max1)