完数定义:一个数的所有因子(包括1)之和等于它自身,这个数就是完数。比如6=1+2+3,6是完数。
本题要求编写程序,计算所有N位完数的个数与列表显示。
函数接口定义:
def wan(n):
在这里解释接口参数。n是一个大于0的正整数。表示几位数。
裁判测试程序样例:
import math
/* 请在这里填写答案 */
n=int(input())
x,lst=wan(n)
print(x)
print(lst)
输入样例:
在这里给出一组输入。例如:
2
输出样例:
在这里给出相应的输出。例如:
1
[28]
代码:
def wan(n):
arr = []
count = 0
for x in range(int(math.pow(10, n - 1)), int(math.pow(10, n))):
sum = 1
for i in range(2, x):
if x % i == 0:
sum += i
if sum == x:
arr.append(sum)
count += 1
return count, arr
编写函数,接收一个正偶数为参数,输出两个素数,并且这两个素数之和等于原来的正偶数。如果存在多组符合条件的素数,则全部输出。
函数接口定义:
在这里描述函数接口。例如:
def evenprimesum( n ):
在这里解释接口参数。n 为传递的整数,不一它是偶数,也不一定是大于0的数据。函数将所有是偶数的素数输出,输出格式为:素数 + 素数 = 偶数,数据之间分隔符为一个空格
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
/* 请在这里填写答案 */
n = int(input())
evenprimesum(n)
输入样例:
在这里给出一组输入。例如:
100
输出样例:
在这里给出相应的输出。例如:
3 + 97 = 100
11 + 89 = 100
17 + 83 = 100
29 + 71 = 100
41 + 59 = 100
47 + 53 = 100
代码:
def Prime(n):
flag = True
for i in range(2, n):
if n % i == 0:
flag = False
break
return flag
def evenprimesum(n):
if n % 2 == 0 and n > 0:
for i in range(3, n // 2 + 1):
if Prime(i) and Prime(n - i):
print(i, '+', n - i, '=', n)
函数接口定义:
def dec2bin(dec_num):
其中 dec_num 是用户传入的十进制浮点数,返回对应的二进制浮点数字符串。
裁判测试程序样例:
/* 你的代码将被嵌在这里 */
if __name__ == '__main__':
dec_num = eval(input())
bin_num = dec2bin(dec_num)
print(bin_num)
输入样例:
在这里给出一组输入。例如:
0.1
输出样例:
在这里给出相应的输出。例如:
0b0.0001100110011001100110011001100110011001100110011001101
代码:
def dec2bin(dec_num):
res = ""
integer = int(dec_num)
res += bin(integer)
if integer == dec_num:
return res
decimal = dec_num - integer
res += "."
while(True):
if decimal == 0:
break
else:
x = decimal * 2
res += str(int(x))
decimal = x - int(x)
return res
函数接口定义:
def moves_elephant(pos):
pos是一个二元元组,为红相或黑象的位置
裁判测试程序样例:
# 返回棋子元组,比如("黑","炮");如为空返回(0,"空")
def getPiece(pos):
return board[pos] # 返回元组
# 返回棋子的颜色
def getColor(piece):
return piece[0]
def moves_empty_board_elephant(pos):
return [(pos[0] - 2, pos[1] - 2), (pos[0] - 2, pos[1] + 2), (pos[0] + 2, pos[1] - 2), (pos[0] + 2, pos[1] + 2)]
# 返回象的合法位置列表
def moves_elephant(pos):
# 请添加你的函数代码
def main():
global board
board = {(0,0):(0,"空"), (0,1):(0,"空"), (0,2):(0,"空"), (0,3):(0,"空"), (0,4):("红","卒"), (0,5):(0,"空"), (0,6):(0,"空"), (0,7):(0,"空"), (0,8):(0,"空"), (0,9):("红","炮"),
(1,0):(0,"空"), (1,1):(0,"空"), (1,2):(0,"空"), (1,3):(0,"空"), (1,4):(0,"空"), (1,5):(0,"空"), (1,6):(0,"空"), (1,7):(0,"空"), (1,8):("红","马"), (1,9):("红","马"),
(2,0):(0,"空"), (2,1):(0,"空"), (2,2):(0,"空"), (2,3):(0,"空"), (2,4):(0,"空"), (2,5):(0,"空"), (2,6):(0,"空"), (2,7):(0,"空"), (2,8):(0,"空"), (2,9):(0,"空"),
(3,0):(0,"空"), (3,1):(0,"空"), (3,2):(0,"空"), (3,3):(0,"空"), (3,4):(0,"空"), (3,5):(0,"空"), (3,6):(0,"空"), (3,7):("黑","士"), (3,8):(0,"空"), (3,9):("黑","炮"),
(4,0):("红","将"), (4,1):("红","士"), (4,2):("红","象"), (4,3):(0,"空"), (4,4):(0,"空"), (4,5):("黑","车"), (4,6):(0,"空"), (4,7):("黑","象"), (4,8):("黑","士"), (4,9):("黑","将"),
(5,0):("红","士"), (5,1):(0,"空"), (5,2):(0,"空"), (5,3):(0,"空"), (5,4):(0,"空"), (5,5):(0,"空"), (5,6):(0,"空"), (5,7):(0,"空"), (5,8):(0,"空"), (5,9):(0,"空"),
(6,0):("红","象"), (6,1):(0,"空"), (6,2):(0,"空"), (6,3):(0,"空"), (6,4):("红","卒"), (6,5):(0,"空"), (6,6):(0,"空"), (6,7):(0,"空"), (6,8):(0,"空"), (6,9):("黑","象"),
(7,0):(0,"空"), (7,1):(0,"空"), (7,2):(0,"空"), (7,3):(0,"空"), (7,4):(0,"空"), (7,5):(0,"空"), (7,6):(0,"空"), (7,7):(0,"空"), (7,8):(0,"空"), (7,9):(0,"空"),
(8,0):(0,"空"), (8,1):(0,"空"), (8,2):(0,"空"), (8,3):(0,"空"), (8,4):("红","卒"), (8,5):(0,"空"), (8,6):(0,"空"), (8,7):(0,"空"), (8,8):(0,"空"), (8,9):(0,"空"),
}
print(*moves_elephant((4,2)))
if __name__ == '__main__':
main()
输入样例:
无输入
输出样例:
(2, 0) (2, 4)
代码:
# 返回象的合法位置列表
def moves_elephant(pos):
# 请添加你的函数代码
# print(getColor(pos))
# print(pos[0], pos[1])
# print(getPiece((pos[0] - 2, pos[1] - 2)))
lis = []
result = getPiece((pos[0] - 2, pos[1] - 2))
# print(result[0])
# print(result[1])
if result[1] == "空":
lis.append((pos[0] - 2, pos[1] - 2))
result = getPiece((pos[0] - 2, pos[1] + 2))
if result[1] == "空":
lis.append((pos[0] - 2, pos[1] + 2))
result = getPiece((pos[0] + 2, pos[1] - 2))
if result[1] == "空":
lis.append((pos[0] + 2, pos[1] - 2))
result = getPiece((pos[0] + 2, pos[1] + 2))
if result[1] == "空":
lis.append((pos[0] + 2, pos[1] + 2))
return lis
在一次酒会上,富翁A遇到了骗子B。骗子对富翁说:“我成立了一个互助基金会,您可以将短期不用的资金存到我这里来,存期30天。第一天您只需要存入1分钱,第二天存入两分钱,依次类推,以后每天存入的钱是前一天的两倍,直到30天期满(含30天)。从您存入钱的第一天开始,您每天可以支取30万元,一直到30天期满(含30天)。”富翁觉得有利可图,欣然同意,两人签订了协议,约定按照上述要求存入资金,并且每天按照最大额度提取资金,合约结束后两人就互不相欠。
请完善下述程序,从键盘读入合约天数以及单日取款金额,计算并输出富翁的盈亏金额。
函数接口定义:
def deposit(n)
def withdraw(n,amount)
注意:仅需提交两个函数的定义代码,不要提交完整程序。
裁判测试程序样例:
#请将两个函数定义在此处
days,amount=eval(input())
diff=deposit(days)-withdraw(days,amount)
if diff>0:
print("富翁亏损: {:.2f}".format(diff))
else:
print("富翁赢利: {:.2f}".format(0-diff))
输入样例:
在这里给出一组输入。例如:
30,300000
输出样例:
在这里给出相应的输出。例如:
富翁亏损: 1737418.23
代码:
def deposit(n):
x = 0.01
count = 0.0
for i in range(0, n):
count += x
x = x * 2
return count
def withdraw(n, amount):
return n * amount
输入样例:
1
3
3 11
5 11
10 12
输出样例:
2
代码:
import math
t = int(input())
while t > 0:
count = 0
n = int(input())
for i in range(0, n):
a, b = map(int, input().split())
if math.gcd(a, b) == 1:
count += 1
print(count)
t -= 1
输入样例:
2
1 7
1 100
输出样例:
none
8 29 50 71 92
代码:
t = int(input())
while t > 0:
a, b = map(int, input().split())
yes = 0
for n in range(a, b + 1):
if n % 3 == 2 and n % 7 == 1 and yes == 0:
yes = 1
print(n, end="")
elif n % 3 == 2 and n % 7 == 1 and yes != 0:
print(" {}".format(n), end="")
if yes == 0:
print("none")
else:
print()
t -= 1
输入样例:
66 78 95 63 85 94 99
输出样例:
[(85, 2.1), (78, 4.9), (94, 11.1), (95, 12.1), (99, 16.1), (66, 16.9), (63, 19.9)]
代码:
grades = list(map(str, input().split()))
num = []
for x in grades:
num.append(eval(x))
avg = sum(num) / len(num)
lis = []
for i in range(len(num)):
lis.append((num[i], round(abs(avg - num[i]), 1)))
print(sorted(lis, key=lambda n: n[1]))
输入样例:
4
3 5 7 15
5 1 2 4 3 5
8 7 15 12 3 4 6 4 9
2 45000 50000
输出样例:
105
60
1260
450000
代码:
import math
# math.lcm(a, b)
t = int(input())
for i in range(0, t):
grades = list(map(int, input().split()))
# grades = sorted(grade, reverse=True)
# print(grades)
x = grades[1]
for j in range(2, len(grades)):
x = math.lcm(x, grades[j])
print(x)
输入样例:
在这里给出一组输入。例如:
1234helll00++++----BBBBB
输出样例:
在这里给出相应的输出。例如:
(5, 5, 6, 8)
代码:
def count_test(st):
cap, small, num, other = 0, 0, 0, 0
for i in st:
if i.isupper():
cap = cap + 1
elif i.islower():
small = small + 1
elif i.isdigit():
num = num + 1
else:
other = other + 1
print("(%d, %d, %d, %d)" % (cap, small, num, other))
string = input()
count_test(string)
输入样例:
2
5
6
输出样例:
25 24 23 22 21
10 9 8 7 20
11 2 1 6 19
12 3 4 5 18
13 14 15 16 17
36 35 34 33 32 31
17 16 15 14 13 30
18 5 4 3 12 29
19 6 1 2 11 28
20 7 8 9 10 27
21 22 23 24 25 26
代码:
def lis(n): # 创建初始数组
arr = [[0] * n for i in range(n)]
return arr
def dfs(arr, x, y, num, n): # 我的思路是和棋盘问题一样,用dfs因为有多种走向问题,但是不完全一样
if n <= 0: return 0
if n == 1:
arr[x][y] = num
return 0
for i in range(n): # 向右
arr[x][y + i] = num
num -= 1
for i in range(n - 1): # 向下
arr[x + 1 + i][y + n - 1] = num
num -= 1
for i in range(n - 1): # 向左
arr[x + n - 1][y + n - 2 - i] = num
num -= 1
for i in range(n - 2): # 向上
arr[x + n - 2 - i][y] = num
num -= 1
dfs(arr, x + 1, y + 1, num, n - 2)
x = int(input())
for i in range(x):
n = int(input())
arr = lis(n)
a = dfs(arr, 0, 0, n * n, n)
# 格式化输出print
l = '%4d' * n
for tmp in arr:
print(l % tuple(tmp))
输入样例:
4
5 3
123 16
0 5
-12 2
输出样例:
5 12
123 7B
0 0
-12 -1100
代码:
def f(n, x):
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F']
b = []
k=0
if n<0:
k=1
n=-n
while True:
s = n // x
y = n % x
b = b + [y]
if s == 0: break
n = s
if k == 1:
print("-", end="")
for i in b[::-1]:
print(a[i], end='')
t = int(input())
for i in range(0, t):
x, y = map(int, input().split())
print("%d " % x, end='')
f(x, y)
print()
输入样例:
5
8
15
输出样例:
3
9
129
代码:
def f(n):
if n < 4:
return 1;
else:
return f(n - 1) + f(n - 3)
while True:
try:
n = int(input())
print(f(n))
except:
break
输入样例:
19
输出样例:
2 3 5 7 11 13 17 19
代码:
def Prime(n):
flag = True
for i in range(2, n):
if n % i == 0:
flag = False
break
return flag
while True:
try:
n = int(input())
k = 0
for i in range(2, n+1):
if Prime(i):
if k == 0:
print(i, end='')
k = 1
else:
print(" %d" % i, end="")
print()
except:
break
输入样例:
在这里给出一组输入。例如:
Hefei Normal University
Anhui Hefei
输出样例:
在这里给出相应的输出。例如:
Hefei
代码:
a=input()
b=input()
f=[[0 for j in range(len(b))] for i in range(len(a))] # 构造二维数组f
for i in range(len(a)):
for j in range(len(b)):
if a[i]!=b[j]:
f[i][j]=0
else:
f[i][j]=f[i-1][j-1]+1
# 维护了f[i][j] 要去找最长的长度
len_max=0
ans=""
for i in range(len(a)):
for j in range(len(b)):
if f[i][j]>len_max:
ans=""
len_max=f[i][j]
for k in range(i-len_max+1,i+1):
ans=ans+a[k]
print(ans)