oploli

用户名oploli
班级
学号202440011959

提交总数 57
通过 35
通过率 61.40%
错误解答 17
时间超限 0
编译错误 5

寻找多数

num = int(input()) lst = list(map(int,input().split())) n = len(lst) set1 = set(lst) ans_list = list(set1) lst2 = [] for i in set1:

lst2.append(lst.count(i))

for i in range(len(lst2)):

if lst2[i] > sum(lst2)/2: print(ans_list[i])

最大子序和

n = int(input()) lst = list(map(int, input().split())) max_global = max_current = lst[0] for i in lst[1:]:

max_current = max(i,max_current+i) if max_current > max_global: max_global = max_current

print(max_global)

换硬币

n = int(input()) two = n five = n seven = n min_coin = float("inf") for t in range(two):

for f in range(five): for s in range(seven): sum = 2*t+5*f+7*s if sum == n: coin_count = t + f + s if coin_count < min_coin: min_coin = coin_count

if min_coin == float("inf"):

print(-1)

else:

print(min_coin)

走网格

def dwg(m,n):

if m == 1 or n == 1 : return 1 else: return dwg(m-1,n) + dwg(m,n-1)

m, n = map(int,input().split()) print(dwg(m,n))

爬楼梯

def stairs(n):

if n == 1 :return 1 if n == 2: return 2 return stairs(n-1) + stairs(n-2)

n = int(input()) print(stairs(n))

背包问题

n, c = map(int, input().split()) W = list(map(int, input().split())) V = list(map(int, input().split())) W = [0] + W V = [0] + V dp = [[0] * (c + 1) for _ in range(n + 1)] for i in range(1, n + 1):

for j in range(1, c + 1): if W[i] > j: dp[i][j] = dp[i - 1][j] # 价值不变 else: dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - W[i]] + V[i])

print(dp[n][c])

最长回文子串

def longest_palindrome(s):

palindromes = [] for i in range(len(s)): for j in range(i + 1, len(s) + 1): substring = s[i:j] if substring == substring[::-1]: palindromes.append(substring) return max(palindromes, key=len, default='')

if __name__ == "__main__":

s = input() print(longest_palindrome(s))

目标和

nums = list(map(int,input().split())) n = int(input()) count = 0 def back(a,b):

global count if a == len(nums): if b == n: count += 1 return back(a + 1, b + nums[a]) back(a + 1, b - nums[a])

back(0,0) print(count)

电话

from itertools import product digits = input() mapping = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'} combinations = [''.join(p) for p in product(*(mapping[d] for d in digits))] print(f"[{', '.join(combinations)}]")

优美排列

n = int(input()) def count(a, b):

if a > n: return 1 return sum(count(a + 1, b | {num}) for num in range(1, n + 1) if num not in b and (num % a == 0 or a % num == 0))

print(count(1, set()))

组合

from itertools import combinations a,b = map(int,input().split()) for i in combinations(range(1,a+1),b):

print(*i)

整数拆分

n = int(input()) if n == 2:print(1) elif n == 3:print(2) else:

q, r = divmod(n, 3)# q是商 r是余数 if r == 0:print(3 ** q) elif r == 1:print(3 ** (q - 1) * 4) # 3^(q-1) * 2*2 else: print(3 ** q * 2)

找假币

n = int(input()) a = list(map(int, input().split())) print(a.index(min(a)) + 1)

分割等和子集

arr1 = list(map(int, input().split())) sums = sum(arr1) if sums % 2 != 0:

print('false')

else:

target = sums // 2 if target in arr1:print('true') else:print('false')

三角形最大周长

A = list(map(int, input().split())) A.sort() n = len(A) for i in range(n-1, 1, -1):

if A[i-2] + A[i-1] > A[i]: print(A[i-2] + A[i-1] + A[i]) exit()

print(0)

未解答 1 Problem