def is_prime(y):
if y < 2:
return False
for i in range(2, int(y**0.5) + 1):
if y % i == 0:
return False
return True
n = int(input())
sum_primes = 0
x = 0
if n < 2:
print("0")
elif n == 2:
print("2\n1")
else:
for i in range(2, n + 1):
if i % 2 == 0 and i != 2:
continue
if sum_primes + i > n:
print(x)
break
if is_prime(i):
print(i)
sum_primes += i
x += 1
P5728
完整代码
def f(x, y): # 判断上下各门之差
return abs(x - y)
n = int(input())
s = []
for i in range(n):
c, m, e = map(int, input().split())
total_sum = c + m + e # 计算每个人的分数和
s.append({'c': c, 'm': m, 'e': e, 'sum': total_sum})
ans = 0
for i in range(n):
for j in range(i + 1, n):
if (f(s[i]['c'], s[j]['c']) <= 5 and
f(s[i]['e'], s[j]['e']) <= 5 and
f(s[i]['m'], s[j]['m']) <= 5 and
f(s[i]['sum'], s[j]['sum']) <= 10): # 符合条件,ans++
ans += 1
print(ans)
P1428
完整代码
n = int(input())
a = list(map(int, input().split()))
sum_arr = [0] * n # 初始化sum数组为0
for i in range(1, n):
for j in range(i, 0, -1):
if a[i] > a[j - 1]:
sum_arr[i] += 1
print(' '.join(map(str, sum_arr))) # 输出
P1319
完整代码
# 获取输入
inputs = list(map(int, input().strip().split()))
# 提取矩阵大小
n = inputs[0]
compressed = inputs[1:]
result = ""
# 开始解压
current_char = "0"
for count in compressed:
result += current_char * count
# 切换当前字符
current_char = "1" if current_char == "0" else "0"
# 将结果输出为的矩阵
for i in range(n):
print(result[i * n:(i + 1) * n])
#二叉树的递归遍历
// 前序遍历递归LC144_二叉树的前序遍历
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result new ArrayList<Integer>(); //也可以把result 作为全局变量,只需要一个函数即可。…