200分值题
- 员工派遣
- 快递员的烦恼
- 符号运算
- 伐木工
- 反射计数
- 分披萨
- 推荐多样性
- 贪心的歌手
- 螺旋数组矩阵(100)
员工派遣
nums = [int(x) for x in input().split(" ")]
x = nums[0]
y = nums[1]
count_x = nums[2]
count_y = nums[3]
left = 1
right = pow(10, 9)
while (True) :if(left >= right):breakelse :mid = (left + right) >> 1target = max(0, count_x - int(mid / y) + int(mid / (x * y))) + max(0, count_y - int(mid / x) + int(mid / (x * y)))if (mid - int(mid / x) - int(mid / y) + int(mid / (x * y)) >= target) :right = midelse :left = mid + 1
print(left)
快递员的烦恼
nums = [int(x) for x in input().split(" ")]
n = nums[0]
r = nums[1]
matrix = [[float('inf') for i in range(n + 1)] for j in range(n + 1)]
distiance_map = [0 for i in range(2000)]
for i in range(n):nums1 = [int(x) for x in input().split(" ")]distiance_map[nums1[0]] = i + 1matrix[0][i + 1] = nums1[1]matrix[i + 1][0] = nums1[1]for i in range(r):nums1 = [int(x) for x in input().split(" ")]matrix[distiance_map[nums1[0]]][distiance_map[nums1[1]]] = nums1[2]matrix[distiance_map[nums1[1]]][distiance_map[nums1[0]]] = nums1[2]#保存客户访问状态,因为有n个客户,所以最多有2的10次方个状态。
cache = [[float('inf') for i in range(n + 1)] for j in range(1024)]#当前访问的客户状态以及距离
queue = []
queue.append([0, 0])
cache[0][0] = 0
while (True):if(len(queue)<=0):breakelse :position = queue.pop(0)print(position[0]);i=0while(True):if(i>n):breakelse :if(i==position[1] or matrix[position[1]][i] ==float('inf')):i+=1continuenew_situation = position[0]if(i > 0):new_situation = position[0] | pow(2, i-1)if (cache[new_situation][i] > cache[position[0]][position[1]] + matrix[position[1]][i]) :cache[new_situation][i] = cache[position[0]][position[1]] + matrix[position[1]][i]queue.append([new_situation, i])i+=1final_distiance = cache[pow(2, n) - 1][0]
if(final_distiance == float('inf')):print(-1)
else :print(final_distiance)
符号运算
nums = []
operations = []#最大公约数
def gcd(a, b) :if(a % b == 0):return b else:return gcd(b, a%b)def eval_op ():global nums,operationsnums1 = nums.pop()nums2 = nums.pop()x = operations.pop()result = [0,0]if(x == '*' or x == '+' or x == '-') :result[0] = nums2[0] * nums1[0]else :result[0] = nums2[0] * nums1[1]sum_a = nums2[1] * nums1[0]sum_b = nums1[1] * nums2[0]if(x == '*'):result[1] = nums2[1] * nums1[1]elif (x == '+'):result[1] = sum_a + sum_belif (x == '-') :result[1] = sum_a - sum_belse :result[1] = nums2[1] * nums1[0]nums.append(result)def get_priority(input_char):if(input_char == '+' or input_char == '-') :return 0else :return 1input_str = input()
nums = []
operations = []
length = len(input_str)
current = ""
i = 0
while (i < length) :if (input_str[i].isdigit()) :while (True) :if(not input_str[i].isdigit()):breakcurrent += input_str[i]if (i + 1 >= length) :breaki+=1nums.append([1, int(current)])current = ""elif (input_str[i] == '(') :operations.append(input_str[i])elif (input_str[i] == ')') :while (operations[0] != '(') :eval_op()operations.pop(0)elif(input_str[i] == ' '):i+=1continueelse :while (len(operations)>0 and operations[0] != '('and get_priority(input_str[i]) <= get_priority(operations[0])) :eval_op()operations.append(input_str[i])i+=1while (True) :if(len(nums) <= 1):breakelse :eval_op()result = nums[0]
if (result[0] == 0) :print("ERROR")
else :up = gcd(result[0], result[1])result[0] /= upresult[1] /= upoutput_str = ""if(result[0] * result[1] < 0):output_str += "-"if (abs(result[0]) == 1) :print(output_str+str(result[1]))else :print(output_str + str(abs(int(result[1]))) + "/" +str(abs(int(result[0]))))
伐木工
import functools
import sys
import copy
import re
import mathn =int(input())
cache = [0 for i in range(n+1)]
cache[0] = 0
memory = [[] for i in range(n+1) ]for i in range(1,n+1):memory[i].append(i)cache[i] = ii=1
while(True):if(i>n):breakelse :for j in range(1, i):if(cache[i] > cache[j] * cache[i - j]):continueelif(cache[i] < cache[j] * cache[i - j]):cache[i] = cache[j] * cache[i - j]memory[i] = []for k in range(len(memory[j])):memory[i].append(memory[j][k])for k in range(len(memory[i-j])):memory[i].append(memory[i-j][k])else :if(len(memory[i]) > len(memory[j]) + len(memory[i-j])):memory[i] = []for k in range(len(memory[j])):memory[i].append(memory[j][k])for k in range(len(memory[i-j])):memory[i].append(memory[i-j][k])i+=1result = memory[n]
result.sort()
output_str = ""
for j in range(len(result)):output_str += str(result[j]) + " "
print(output_str)
反射计数
import mathnums = [int(x) for x in input().split(" ")]
w = nums[0]
h = nums[1]
x = nums[2]
y = nums[3]
sx = nums[4]
sy = nums[5]
t = nums[6]
matrix = [[0 for i in range(w)] for j in range(h)]
for i in range(h):inupt_str = input()for j in range(w):matrix[i][j] = ord(inupt_str[j]) - ord('0')result = 0
while (True):if(t<0):breakelse :if ((x + sx < 0) or (x + sx > w - 1)) :sx = -sxelif ((y + sy < 0) or (y + sy > h - 1)) :sy = -syif (matrix[y][x] == 1) :result+=1x += sxy += syt-=1
print(result)
分披萨
n =int(input())
nums = []
for i in range(n):nums.append(int(input()))
memory = [[0 for i in range(n)] for j in range(n)]def backtrace(left, right) :global n,nums,memoryif(nums[left] > nums[right]):left = (left + n + 1) % nelif(nums[left] < nums[right]):right = (right + n - 1) % nif (memory[left][right] <= 0) :if (left == right) :memory[left][right] = nums[left]else :new_left = (left + 1) % nnew_right = (right + n - 1) % nmemory[left][right] = nums[left] + backtrace(new_left, right)if(nums[right] + backtrace(left,new_right) > memory[left][right]):memory[left][right] = nums[right] + backtrace(left,new_right)return memory[left][right]else :return memory[left][right]result = 0
i=0
while(True):if(i>=n):breakelse :left = (i + n + 1) % nright = (i + n - 1) % ntarget = backtrace(left, right)if(target+nums[i] > result):result = target+nums[i]i+=1
print(result)
推荐多样性
n =int(input())
m = int(input())
input_matrix = []
while(True):try:input_matrix.append([int(x) for x in input().split(" ")])except:break
output_length = [0 for x in range(len(input_matrix))]
nums = []
while (True) :if(len(nums) >= n * m):breakelse :for i in range(len(input_matrix)):index = len(input_matrix[i])if(len(input_matrix[i]) > output_length[i] + 4):index = output_length[i] + 4j = output_length[i]while(j<index):nums.append(input_matrix[i][j])j+=1output_length[i] = index
output_str = ""
for j in range(n):for i in range(m):output_str += str(nums[i * n + j]) + " "
print(output_str[:-1])
贪心的歌手
params = [int(x) for x in input().split(" ")]
T = params[0]
N = params[1]
nums = [int(x) for x in input().split(" ")]
matrix = []
for i in range(N):matrix.append([int(x) for x in input().split(" ")])for i in range(len(nums)):T-=nums[i]moneys = []
i=0
while(True):if(i>=N):breakelse:temp1 = matrix[i][0]temp2 = matrix[i][1]j=0while(True):if(j>=T or temp1<=0):breakelse:moneys.append(temp1)temp1 = temp1- temp2i+=1moneys = sorted(moneys, key=lambda x:-x)result = 0
k = 0
while(True):if(k>=T):breakelse:result += moneys[k]k+=1
print(result)
螺旋数组矩阵(100)
directions = [0, 1, 0, -1, 0]
params = [int(x) for x in input().split(" ")]
k = params[0]
n = params[1] matrix = [["*" for i in range(int((k - 1) / n + 1)) ] for j in range(n)]
start_x = 0
start_y = 0
count = 1
index = 0 while (True):if(count > k):breakelse :matrix[start_x][start_y] = str(count)count+=1new_x = start_x + directions[index]new_y = start_y + directions[index+1]if (new_x < 0 or new_x >= n or new_y < 0 or new_y >= (k - 1) / n +1 or matrix[new_x][new_y]!="*") :index = (index + 1) % 4start_x = start_x + directions[index]start_y = start_y + directions[index+1]else :start_x= new_xstart_y= new_yfor i in range(n):output_str = ""for j in range(int((k - 1) / n + 1)):output_str += matrix[i][j]if (j != (k - 1) / n):output_str += " "print(output_str)