suibian
链接:https://www.nowcoder.com/questionTerminal/fc72d3493d7e4be883e931d507352a4a?f=discussion
来源:牛客网
定义一个方向数组
1
char[] direction = new char[] {'E', 'S', 'W', 'N', 'E', 'S', 'W'};
然后分别计算Left和Right的步数,并将其%4,最后direction[3 + (countRight - countLeft)]就是所求方向。
n = int(input())
dir = input()
s = ['N','E','S','W']
s1 = ['N','W','S','E']
if n>0:
L=0
R=0
for i in dir:
if i == "L":
L += 1
else:
R += 1
if R - L >= 0:
print(s[(R-L)%4])
else:
print(s1[(L-R)%4])
else:
print('N')
链接:https://www.nowcoder.com/questionTerminal/bac5a2372e204b2ab04cc437db76dc4f?f=discussion
来源:牛客网
朴素的做法是枚举n^2个点然后跟k作比较。这显然对n<=100000的规模来说是不允许通过的。
注意到当除数是y时,当x=1 to n时,余数是1,2,3,...,y-1,0循环出现,循环节长度显然是y
那么我们可以枚举y=k to n(当y<k时所有余数均小于k,因此不需要考虑)
然后对于x=1~n,总共出现了[n/y]个循环节,然后数出每个循环节里面不小于k的余数。最后再数出不满一个循环节的不小于k的余数,就是答案了。注意当k=0的时候由于余数0出现在循环的末尾,因此要特别判断。
复杂度为O(n)
temp = list(map(int, input().split()))
n = temp[0]
k = temp[1]
if k == 0:
ans = n * n # k=0是比较特殊的
else:
ans = 0
for y in range(k + 1, n + 1):
# 从每一列来看,根据每y个一个循环的规律,快速计算余数矩阵余数值
# cycle = [i for i in range(1, y)] + [0] # 循环部分
satisfy_num = y - k # 一个循环中满足的组合个数
cycle_num = n // y # 完整循环个数
res_num = n % y # 剩余不完整部分循环中的元素个数
ans += satisfy_num * cycle_num + max(res_num - k + 1, 0) # 注意这里最差就是不完整部分满足个数为0,但是不能为负数
print(ans) ss
l,r = list(map(int,input().split()))
def not_in_1_to_x(x):#close 1 to 4 has two
return (x+2)//3
print(r-l+1-not_in_1_to_x(r)+not_in_1_to_x(l-1)) #l-1
上式
import sys
import bisect
task = {}
lines = sys.stdin.readlines()
n, m = map(int, lines[0].strip().split())
for line in lines[1:-1]:
if not line.strip().split():
continue
a, b = map(int, line.strip().split())
task[a] = max(task.get(a, 0), b)
arr = sorted(task.keys())
for i in range(1, len(arr)):
if task[arr[i]] < task[arr[i -1]]:
task[arr[i]] = task[arr[i -1]]
skills = map(int, lines[-1].strip().split())
for skill in skills:
if skill in task:
print(task[skill])
else:
ind = bisect.bisect(arr, skill)
if ind == 0:
print(0)
else:
print(task[arr[ind -1]])
python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值。
for i in range(n_job):
diff,wage = list(map(int,input().strip().split()))
diff_to_wage[diff] = max(diff_to_wage.get(diff,0),wage)
import bisect
n_job,m = list(map(int,input().strip().split()))
diff_to_wage = {}
i = 0
while i < n_job:
*** = list(map(int,input().strip().split()))
if ***:
diff,wage = ***
diff_to_wage[diff] = max(diff_to_wage.get(diff,0),wage)
i+=1
powers = []
while not powers:
powers = list(map(int,input().strip().split())) #*** again!!
keys = sorted(diff_to_wage.keys())
for i in range(1,len(keys)):
if diff_to_wage[keys[i]] < diff_to_wage[keys[i-1]]:
diff_to_wage[keys[i]] = diff_to_wage[keys[i-1]]
for power in powers:
if power in diff_to_wage: #in key time out 40%
print(diff_to_wage[power])
else:
ind = bisect.bisect(keys,power)
if not ind:
print(0)
else:
print(diff_to_wage[keys[ind-1]])
gg
n = int(input())
dir_str = input()
L,R=0,0
L4 = ['N','W','S','E']
R4 = ['N','E','S','W']
for ch in dir_str:
if ch == "L":
L+=1
else:
R+=1
if L>R:
print(L4[(L-R)%4])
else:
print(R4[(R-L)%4])count()方法语法:
str.count(sub, start= 0,end=len(string))
参数
sub -- 搜索的子字符串
start -- 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
end -- 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
n = int(input())
dir_str = input()
L,R=0,0
L4 = ['N','W','S','E']
R4 = ['N','E','S','W']
L = dir_str.count('L')
R = dir_str.count('R')
if L>R:
print(L4[(L-R)%4])
else:
print(R4[(R-L)%4])
路灯
if __name__ == '__main__':
count = int(input()) # 测试用例的个数
n = []
lantern = []
for i in range(count):
n_tmp = int(input()) # 路灯个数
n.append(n_tmp)
lantern_tmp = input() # 路灯分布字符串
lantern.append(lantern_tmp)
lantern_count = [0 for i in range(count)] # 存储最终结果的数组
for i in range(len(lantern)): # 循环路灯数
j = 0
while (j < len(lantern[i])): # 循环对应路灯排列字符串
if lantern[i][j] == '.':
j += 3
lantern_count[i] += 1
else:
j += 1
print(lantern_count[0])
for i in range(len(lantern_count) - 1):
print(lantern_count[i + 1])
2019年10月14日22:22:01
查找最晚入职员工的所有信息
select * from employees where hire_date = (select max(hire_date) from employees ) select * from employees where hire_date = (select max(hire_date) from employees)
查找入职员工时间排名倒数第三的员工所有信息
LIMIT m,n : 表示从第m+1条开始,取n条数据;
LIMIT n : 表示从第0条开始,取n条数据,是limit(0,n)的缩写。
select * from employees where hire_date = ( select distinct hire_date from employees order by hire_date desc limit 2,1 )


