1。万万没想到之聪明的编辑
添加链接描述
import sys
n = int(sys.stdin.readline()[:-1])
input = []
for i in range (n):
input.append(sys.stdin.readline()[:-1])
#print(input)
result = []
for inp in input:
length = len(inp)
res = inp[:2]
if length >= 3:
for i in range(2, length):
#print('inp ', inp)
#print('res ', res)
if inp[i] == res[-1] and res[-1] == res[-2]:
continue
elif inp[i] == res[-1] and res[-2] == res[-3]:
continue
res += inp[i]
result.append(res)
for res in result:
print(res)
2.万万没想到之抓捕孔连顺
添加链接描述
import sys
n,d = map(int, input().split())
nums = list(map(int, input().split()))
#nums = (sys.stdin.readline()[:-1].split())
#nums = [int(f) for f in nums]
left, right, res = 0, 2, 0
while left < n-2:
while right < n and nums[right] - nums[left] <= d:
right += 1
num = right - 1 - left
res += num * (num - 1) // 2 #计算left到right之间的点k的组合数C(k, 2)
left += 1
print(res % 99997867)
3.雀魂启动!
添加链接描述
def isHu(nums):
if not nums:
return True
n = len(nums)
count = nums.count(nums[0])
# 没出现过雀头,且第一个数字出现的次数 >= 2,去掉雀头剩下的能不能和牌
if n%3 and count >= 2 and isHu(nums[2:]):
return True
# 如果第一个数字出现次数 >= 3,去掉这个刻子后看剩下的能和牌
if count >=3 and isHu(nums[3:]):
return True
# 如果存在顺子,移除顺子后剩下的能和牌
if nums[0]+1 in nums and nums[0]+2 in nums:
tmp = list(nums) #保存现场
tmp.remove(nums[0])
tmp.remove(nums[0]+1)
tmp.remove(nums[0]+2)
if isHu(tmp):
return True
return False
def main(nums):
d = {} #统计出现次数(词频)
for i in nums:
d[i] = d.get(i, 0) + 1
card_list = set(range(1, 10)) - {k for k, v in d.items() if v == 4} #只取次数为4的,用来做差集
res = []
for i in card_list:
if isHu(sorted(nums + [i])):
res.append(i)
print(' '.join(str(r) for r in sorted(res)) if res else '0')
nums = [int(x) for x in input().split()]
main(nums)
4.特征提取
添加链接描述
case = int(input())
res = 0
for _ in range(case): #遍历每个用例
frames = int(input())
last_map = {} #上一帧频率
for _ in range(frames): #遍历每帧
line = list(map(int, input().split(' '))) #每行转成数组
n = line.pop(0) #特征数
current_map = {} #记录特征频率
for i in range(n): #取出每帧所有特征
feature = (line[2*i], line[2*i+1])
#print(feature)
current_map[feature] = last_map.get(feature, 0) + 1 #统计特征频率
res = max(res, current_map[feature]) #记录最大连续帧
#print(last_map, current_map)
last_map = current_map #更新
print(res if res >= 2 else 1)
5.毕业旅行问题
添加链接描述
dfs回溯,没有完全通过
import sys
n = int(input())
m = []
for i in range(n):
m.append(list(map(int, input().split(' '))))
#print(m)
vis = [False] * n
vis[0] = True
res = sys.maxsize
def abc():
print(m)
def dfs(vis, local, vn, price):
global res
#print(res)
if price > res:
return
if vn == n:
val = price + m[local][0]
res = min(res, val)
return
for i in range(1, n):
if vis[i]:
continue
vis[i] = True
dfs(vis, i, vn+1, price + m[local][i])
vis[i] = False
#abc()
dfs(vis, 0, 1, 0)
print(res)
dp:
添加链接描述
6.找零
添加链接描述
num = 1024 - int(input())
coin_64 = num // 64
num = num % 64
coin_16 = num // 16
num = num % 16
coin_4 = num // 4
num = num % 4
coin_1 = num
print(coin_64 + coin_16 + coin_4 + coin_1)
7.机器人跳跃问题
添加链接描述
一维dp:
input()
arr = list(map(int, input().split(' ')))
# 假设跳跃前能力为E,要跳的高度为H,那么跳跃后的能量就是2E-H,
# 那么跳跃后的能量加上高度就是跳跃前的两倍,然后从后往前逆推。
E = 0 # 跳到最后一步的能力值设为0
#从后往前
#递推公式为E(k) = [E(k+1)+H(k+1)] / 2
#由于E都是整数,因此考虑向上取整,故实际运算时用[E(k+1)+H(k+1)+1] // 2
for H in arr[::-1]:
E = (E + H + 1) >> 1 #用右移运算代替对2整数除也可
print(E)