第14届 二期 Python

具体题目及解析可点击本链接

1题(填空题)

答案:2048

2题(填空题)

代码

sum =0
for i in range(1950,2022):
    if (i %4 == 0 and i%100 != 0 )or i % 400 ==0:
        sum +=366
    else:
        sum +=365
sum=sum+31+30+31
print(sum)
#Output
#26390

答案:26390

3题(填空题)

代码(Python)

```python
def six(number):
    long=len(str(number))
    l=[]
    num=0
    for i in range(1,long+1):
        num=number%10
        l.append(num)
        number=int(number/10)
    result=0
    for i in range(0,long):
        result= result+l[i]*pow(16,i)
    return result
for i in range(10,10000):
    res=six(i)
    if res%i==0:
        print(i)
        break
#Output
# 1038

答案:1038

4题(填空题)

答案:592

a = []
with open('text.txt', 'r',encoding='UTF-8') as f :
    a = f.readlines()
f = [[0] * 70 for _ in range(70)]
for i in range(1, 31) :
    for j in range(1, 61) :
        if (i - 1) >= 0 and (j - 1) >= 0 :
            f[i][j] = max(f[i - 1][j], f[i][j - 1]) + int(a[i - 1][j - 1])
        elif i - 1 >= 0 :
           f[i][j] =  f[i - 1][j] + int(a[i - 1][j - 1])
        elif j - 1 >= 0 :
            f[i][j] =  f[i][j - 1] + int(a[i - 1][j - 1])
print(f[30][60])

5题(填空题)

答案:33

代码(Python)

n = 2022
 
prime = []
st = [True] * 2030 
def get_prime(n) :
    for i in range(2, n + 1) :
        if st[i] :
            prime.append(i)
 
       for j in prime :
            if i * j > n :
                break
            st[i * j] = False
            if i % j == 0 :
                break
get_prime(n)
f = [-100010] * 2030
f[0] = 0
for i in range(1, len(prime) + 1 ) :
    for j in range(2022, prime[i - 1] - 1, -1) :
        f[j] = max(f[j], f[j - prime[i - 1]] + 1)

print(f[2022])

6题

t = int(input())
c = int(input())
s = int(input())
speed = float(c/t)
surplus = s-c
res = int(surplus/speed)
print(res)

7题

temp = []
list = []
n = int(input())
for i in range(n):
    list.append(input())
for k in range(n):
    if k not in temp:
        temp.append(list[k])
print(temp)

8题

算法

定义函数check和nixu,check的作用是用来检查字符串是否是回文,nixu函数是将字符串转换成列表并逆序输出成字符串。首先添加首字母,如字符串“abc”,首先添加首字母‘a’,检查是否回文,再添加字符串“ba”,检查是否回文,满足则将其输出。

代码(Python)

def check(str):
    if (str == str[::-1]):
        return 1
    else:
        return 0
def nixu(str):
    a =list(str)
    a.reverse()
    return (''.join(a))
str = input("请输入一个字符:")
res = []
temp = []
n =len(str)
if n==1:
    res = str
else:
    if (check(str)):
        res = str
    else:
            temp = list(str)
            print(temp)
            for i in range(n):
                Str=str
                Str += nixu(temp[0:i+1])
                print("Str",Str)
                if (check(Str)):
                  res = Str
                  break

print(res)

9题

算法(枚举)

N = 110

a = [[0] * N for _ in range(N)]
 
n, m = map(int, input().split())
 
for i in range(1, n + 1) :
    a[i][1 : m + 1] = list(input())
 
def check(i, j, ix, jy, len) :
    target = a[i][j]
    x, y= i, j
    for k in range(1, len + 1) :
        x = x + ix
        y = y + jy
        if x < 1 or x > n or y < 1 or y > n :
            return False
        if a[x][y] != target :
            return False
    return True
 
res = 0
for i in range(1, n + 1) :
    for j in range(1, m + 1) :
        for len in range(1, min(n, m) // 2 + 1):
            if (check(i, j, -1, -1, len) and check(i, j, -1, 1, len) and check(i, j, 1, 1, len) and check(i, j, 1, -1, len)) :
                res += 1
 
print(res)
 

10题

算法

  • 暴力求解
def ch(n1,n2):
    number=n1;
    n1=n2;
    n2=number
    return n1,n2
n=int(input())
l=[]
str=input()
l=str.split(' ')
#print(l)
dj=0
for i in range(0,n):
    for j in range(i+1,n):
        if l[i]>l[j]:
            (l[i],l[j])=ch(l[i],l[j])
            dj=dj+int(max(l[i],l[j]))
print(dj)

你可能感兴趣的:(python,开发语言,数据挖掘,学习)