试题链接:
link.
试题A:门牌制作
代码:
ans = []
for i in range(1,2020+1):
if '2' in str(i):
ans.append(str(i).count('2'))
print(sum(ans))
结果:624
试题B:既约分数
代码:
def gcd(x,y):
if y==0:
return x
else:
return gcd(y,x%y)
ans = 0
for i in range(1,2020+1):
for j in range(1,2020+1):
if gcd(i,j) == 1:
ans += 1
print(ans)
结果:2481215
试题C:蛇形填数
代码:
ans = 1
for i in range(20):
ans += i * 4
print(ans)
结果:761 链接: link.
试题D:跑步锻炼
代码:
from datetime import date
from datetime import timedelta
start = date(2000, 1, 1)
end = date(2020, 10, 2)
res = 0
while start < end:
res += 2 if start.day == 1 or start.weekday() == 0 else 1
start += timedelta(days=1)
print(res)
结果:8879 链接: link.
试题E:七段码
代码:
import numpy as np
import itertools as it
class UnionFind:
def __init__(self, n):
self.father = list(range(n))
self.size = [1] * n
self.setCount = n
def find(self, x):
if self.father[x] == x:
return x
self.father[x] = self.find(self.father[x])
return self.father[x]
def merge(self, x, y):
x, y = self.find(x), self.find(y)
if x == y:
return False
if self.size[x] < self.size[y]:
x, y = y, x
self.father[y] = x
self.size[x] += self.size[y]
self.setCount -= 1
return True
res = 0
data = list(np.zeros((7, 7)))
data[0][1] = data[0][5] = 1
data[1][0] = data[1][6] = data[1][2] = 1
data[2][1] = data[2][3] = data[2][6] = 1
data[3][2] = data[3][4] = 1
data[4][3] = data[4][5] = data[4][6] = 1
data[5][0] = data[5][4] = data[5][6] = 1
num = [i for i in range(0, 7)]
for i in range(1, 8):
total = it.combinations(num, i)
for j in total:
uf = UnionFind(7)
for z in j:
for k in range(len(data[z])):
z = int(z)
b = int(data[z][k])
if b == 1 and k in j:
uf.merge(z, k)
if uf.setCount == 7 - len(j) + 1:
res += 1
print(res)
结果:80 链接: link.
试题F:成绩统计
代码:
n = int(input())
num1 = 0
num2 = 0
for i in range(n):
stu = int(input())
if stu >= 60:
num1 += 1
if stu >= 85:
num2 += 1
print('{:d}%'.format(round(num1/n*100)))
print('{:d}%'.format(round(num2/n*100)))
链接: link.
试题G:回文日期
代码:
st_time=input()
year=int(st_time[0:4])
big_month=[1,3,5,7,8,10,12]
flag1=0
flag=0
while(True):
s=str(year)
time=s+s[::-1]
month=int(time[4:6])
day=int(time[6:8])
if(flag==0):
if(month<int(st_time[4:6]) or day<int(st_time[6:8])):
flag=1
year+=1
continue
if(big_month.count(month)==1):
if(month<=12 and month>0 and day<=31 and day>0 and flag1==0):
time1=time
flag1=1
if(month<=12 and month>0 and day<=31 and day>0
and s[0]==s[2] and s[1]==s[3]):
time2=time
break
else:
if(month==2):
if(year%400==0 or (year%4==0 and year%100!=0)):
if(month<=12 and month>0 and day<=29 and day>0 and flag1==0):
time1=time
flag1=1
if(month<=12 and month>0 and day<=29 and day>0
and s[0]==s[2] and s[1]==s[3]):
time2=time
break
else:
if(month<=12 and month>0 and day<=28 and day>0 and flag1==0):
time1=time
flag1=1
if(month<=12 and month>0 and day<=28 and day>0
and s[0]==s[2] and s[1]==s[3]):
time2=time
break
else:
if(month<=12 and month>0 and day<=30 and day>0 and flag1==0):
time1=time
flag1=1
if(month<=12 and month>0 and day<=30 and day>0
and s[0]==s[2] and s[1]==s[3]):
time2=time
break
year+=1
flag=1
print(int(time1))
print(int(time2))
链接: link.
试题H:子串分值和
代码:
list1=list(input())
list2=[-1 for i in range(26)]
count=0
for i in range(len(list1)):
index=ord(list1[i])-ord('a')
count+=(len(list1)-i)*(i-list2[index])
list2[index]=i
print(count)
链接: link.
试题I:平面切分
代码:
N=int(input())
input_lis=[]
crosspoint_x_set=set()
for i in range(N):
input_lis.append(list(map(int,input().split())))
input_lis=list(set([tuple(t) for t in input_lis]))
each_line_num_lis = [1] * (len(input_lis) + 1)
for i in range(1,len(input_lis)):
crosspoint_x_set.clear()
for n in range(i):
if input_lis[i-n-1][0]!=input_lis[i][0]:
x=round((input_lis[i][1]-input_lis[i-n-1][1])/(input_lis[i-n-1][0]-input_lis[i][0]),4)
crosspoint_x_set.add(x)
each_line_num_lis[i] += len(crosspoint_x_set)
print(sum(each_line_num_lis) )
链接: link.
试题J:子串排序
代码:
|