比赛链接:https://ac.nowcoder.com/acm/contest/75174#question
A.小红的01背包
思路:
V//x是看能装多少物品
再乘上每件物品的价格y就可以了
v,x,y = map(int,input().split())
print((v//x)*y)
B.小红的dfs
思路:
就是看每行每列不符合条件的有多少,先看第一行和第一列记录cnt1,再看第二行和第二列记录cnt2,再看第三行和第三列记录cnt3,看哪个符合条件最多。
list1 = []
for i in range(3):
tmp = list(input())
list1.append(tmp)
cnt1 = 0
cnt2 = 0
cnt3 = 0
if list1[0][0] != 'd': cnt1 += 1
if list1[0][1] != 'f': cnt1 += 1
if list1[0][2] != 's': cnt1 += 1
if list1[1][0] != 'f': cnt1 += 1
if list1[2][0] != 's': cnt1 += 1
if list1[1][0] != 'd': cnt2 += 1
if list1[1][1] != 'f': cnt2 += 1
if list1[1][2] != 's': cnt2 += 1
if list1[0][1] != 'd': cnt2 += 1
if list1[2][1] != 's': cnt2 += 1
if list1[2][0] != 'd': cnt3 += 1
if list1[2][1] != 'f': cnt3 += 1
if list1[2][2] != 's': cnt3 += 1
if list1[0][2] != 'd': cnt3 += 1
if list1[1][2] != 'f': cnt3 += 1
print(min(cnt1,cnt2,cnt3))
C.小红的排列生成
思路:
可以加1减1,先排列,那么我们只要看每次要达成条件每位的数应该如何变化就可以
n = int(input())
a = list(map(int,input().split()))
a.sort()
sum1 = 0
for i in range(1,n+1):
sum1 += abs(i - a[i-1])
print(sum1)