说明
笔者做的测试的卷子
1
回文串判断,就是给定一个字符串,判断最少修改几个才能变成回文串,就从两边向中间遍历就行
N = int(input()) str_array = list(input()) left,right = 0,N-1 res = 0 while left < right: if str_array[left] != str_array[right]: res += 1 left += 1 right -= 1 print(res)
2.
铺砖快使得相邻的颜色不同,并且满足所有颜色的砖块个数相同
没写出来,只A了0.1,求个大佬帮看看
def calc_euclid(N,M): if N % M != 0: return calc_euclid(M,N%M) return M T = int(input()) while T: T -= 1 [N,M] = list(map(int,input().split())) if N % 2 == 0 or M % 2 == 0: print(2) elif N == M: print(N) # elif (N%3==0 and M%3==0) or (M%3==0 and N%3==0): # print(3) else: val = calc_euclid(N,M) print(max(N//val,M//val))
3
拆开一个数字n,使得其拆出来的数字的和能组成[1,n]的所有数字,
第一想法就是不断除以2,因为要使和达到n,至少得有ceil(n/2)
才行
from math import ceil T = int(input()) while T: T -= 1 res = 0 n = int(input()) while n > 1: divid = ceil(n/2) res += 1 n -= divid print(res+1)
4
对一个矩阵,每次取最大行/列,以求出k次的最大和,每次取完后该行/列的值减去d
暴力法,用两个数组存了行和和列和,并找最小的,迭代k次。只A了40.
N,M,k,d = list(map(int,input().split())) nums = [] row_sum = [] col_sum = [] for _ in range(N): val = list(map(int,input().split())) nums.append(val) row_sum.append(sum(val)) for j in range(M): val = 0 for i in range(N): val += nums[i][j] col_sum.append(val) all_sum = 0 while k : k-= 1 max_row_sum = max(row_sum) max_col_sum = max(col_sum) if max_row_sum > max_col_sum or (max_row_sum == max_col_sum and N > M): row_idx = row_sum.index(max_row_sum) all_sum += max_row_sum row_sum[row_idx] -= M*d for j in range(M): col_sum[j] -= d else: col_idx = col_sum.index(max_col_sum) all_sum += max_col_sum col_sum[col_idx] -= N*d for i in range(N): row_sum[i] -= d print(all_sum)
总结
我好菜啊
全部评论
(6) 回帖