python 的字符串数学表达式计算题,如果时间不够使用:
eval(str)
有一个长为n的数组A,求满足0≤a≤b<n的A[b]-A[a]的最大值。 给定数组A及它的大小n,请返回最大差值。
# -*- coding:utf-8 -*-
class LongestDistance:
def getDis(self, A, n):
# write code here
maxdiff = 0
minValue = A[0]
for i in xrange(n):
minValue = min(minValue,A[i])
maxdiff = max(maxdiff,A[i]-minValue)
return maxdiff
输入一个包含6个数字的数据,输出24时间进制的数值:
list1 = ['0','2','3','0','5','6']
all_index = []
all_num = []
exclude = []
time = []
max1 = 0
max2 = 0
max3 = 0
tmp = [0,0]
a = 0
for i in range(6):
for j in range(i+1,6):
all_index.extend([[i,j],[j,i]])
print(all_index)
for index in all_index:
num = list1[index[0]] + list1[index[1]]
num = int(num)
if num >= max1 and num <= 24:
max1 = num
tmp[0]=index[0]
tmp[1]=index[1]
a +=1
exclude.extend(tmp)
for index in all_index:
num = list1[index[0]] + list1[index[1]]
num = int(num)
print(num)
if num >= max2 and num <= 59 and (index[0] not in exclude) and (index[1] not in exclude):
max2 = num
tmp[0]=index[0]
tmp[1]=index[1]
a +=1
exclude.extend(tmp)
for index in all_index:
num = list1[index[0]] + list1[index[1]]
num = int(num)
if num >= max3 and num <= 60 and (index[0] not in exclude) and (index[1] not in exclude):
max3 = num
a +=1
if a>2:
print(max1, max2, max3)
print(exclude)
else:
print('invalid')
牛客python3 大佬的刷题记录:
华科平凡
华为笔试题
有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。
输入描述:
每组数据为一行一个整数n(小于等于1000),为数组成员数,如果大于1000,则对a[999]进行计算。
输出描述:
一行输出最后一个被删掉的数的原始下标位置。
示例1
输入
8
输出
6
约瑟夫环解法
while True:
try:
n = int(input())
except:
exit()
a = list(range(n))
i = 0
while len(a) > 1:
i = (i+2) % len(a)
if i != len(a) - 1:
a = a[:i] + a[i+1:]
else:
a = a[:i]
i = 0
print (a[0])