蛇形填充n阶方阵
题目要求在一个 ( n \times n ) 的方阵中填入从 1 到 ( n \times n ) 的数字,并且按照蛇形顺序从右上角开始,沿着方阵的边界顺时针进行填充。蛇形填充的特殊排列方式使得每一层数字呈现出波浪形的排列方式。
例如,当 ( n = 4 ) 时,方阵应如下所示:
plaintext
10 11 12 1
9 16 13 2
8 15 14 3
7 6 5 4
你需要编写程序输出填充后的方阵,确保格式的整齐性。
def solution(n: int) -> list:
# write code here
res = [[0 for _ in range(n)] for _ in range(n)]
move = [1, 0, -1, 0, 1]
m = 0
x, y = -1, n-1
for i in range(1, n * n + 1):
nx, ny = x+move[m], y+move[m+1]
# print(nx, ny)
# print(res[nx][ny])
# print(res)
if nx < 0 or nx >= n or ny < 0 or ny >= n or res[nx][ny]:
# print(nx, ny)
m = (m+1) % 4
nx, ny = x+move[m], y+move[m+1]
x, y = nx, ny
res[x][y] = i
# print(res)
# print(x, y)
return res
if __name__ == '__main__':
print(solution(4) == [[10, 11, 12, 1], [
9, 16, 13, 2], [8, 15, 14, 3], [7, 6, 5, 4]])
print(solution(5) == [[13, 14, 15, 16, 1], [12, 23, 24, 17, 2], [
11, 22, 25, 18, 3], [10, 21, 20, 19, 4], [9, 8, 7, 6, 5]])
print(solution(3) == [[7, 8, 1], [6, 9, 2], [5, 4, 3]])
选择题反选效果分析
小U正在检查某同学的选择题答案。试卷共有 **n** 道题目,每道题目只有两个选项 **A** 和 **B**。当前小U手上有两组答案:
**s**:该同学的原始答案。**t**:标准答案。
小U想知道,如果将该同学的所有答案都反选(即:如果某题的答案是 **A** 则改成 **B**,如果是 **B** 则改成 **A**),那么在反选之后,正确的答案数量是否会增加?具体结果有三种可能:
- 如果反选后的正确答案数 增加,输出 "yes"。
- 如果反选后的正确答案数 不变,输出 "draw"。
- 如果反选后的正确答案数 减少,输出 "no"。
测试样例:
样例1:
输入:**n = 2, s = "AB", t = "AA"**
输出:**'draw'**
样例2:
输入:**n = 3, s = "BAA", t = "ABB"**
输出:**'yes'**
样例3:
输入:**n = 4, s = "ABAB", t = "BABA"**
输出:**'yes'**
def solution(n: int, s: str, t: str) -> str:
# write code here
count = 0
for i in range(n):
if s[i] == t[i]:
count += 1
if count > n / 2:
return "no"
elif count == n / 2:
return "draw"
else:
return "yes"
pass
if __name__ == '__main__':
print(solution(2, "AB", "AA") == 'draw')
print(solution(3, "BAA", "ABB") == 'yes')
print(solution(4, "ABAB", "BABA") == 'yes')
消息队列
- 系统崩溃
- 服务处理能力有限
- 链路耗时长尾
- 日志如何处理
什么是消息队列
消息队列(M Q),指保存消息的一个容器,本质是一个队列。但是这个队列呢,需要支持高吞吐,高并发,并且高可