No.11 很简单
一般pta的输入都是以‘ ’相隔的,所以两个变量的话,可以使用map(int,input().split())进行序列解包
n=int(input())
for i in range(n):
a,b,c=map(int,input().split())
if a+b>c:
print("Case #{}: true".format(i+1))
else:
print("Case #{}: false".format(i+1))
NO.12 复杂类的题
round(数字,保留的位数)同时具有四舍五入的供能
- 错误1:if else的分支设错,因为在第一个if中使用的是i%10==0,在最后的else中没有排除i%5==0的情况,导致不符合题意
- 错误二:(隐蔽)一开始假设的交叉差为0,若最后也是0,则视为该列不存在,实际情况是可能交叉之后计算的差为0,是存在的,利用字符'0'与数字0不相等解决
- pta的问题,“ ”.join(只能是字符串列表)
def everysub(list2):
cnt=0
for i in range(len(list2)):
if i%2==0:
cnt+=list2[i]
else:
cnt-=list2[i]
return cnt
def my_append(i,list2):
if i==0:
list2.append('N')
else :
list2.append(str(i))
input_list=[int(i) for i in input().split()]
cnt1,list2,num,sum_,num_,max_=0,[],0,0,0,0
for i in range(1,len(input_list)):
if input_list[i]%10==0:
cnt1+=input_list[i]
elif input_list[i]%5==1:
list2.append(input_list[i])
elif input_list[i]%5==2:
num+=1
elif input_list[i]%5==3:
sum_+=input_list[i]
num_+=1
else:
if input_list[i]%5== 4 and max_<input_list[i]:
max_=input_list[i]
ans=[]
ave=0.0
if num_:
ave=round(sum_/num_,1)
#ave=int(sum_*10/num_+0.5)/10
#cnt1,list2,num,sum_,max_
my_append(cnt1,ans)
if len(list2)>0:
my_append(str(everysub(list2)),ans)
else:
my_append(0,ans)
my_append(num,ans)
my_append(ave,ans)
my_append(max_,ans)
print(" ".join(ans))
NO.13 素数问题,其实素数的分布挺广泛的
非特殊情况都是用打表法做,20W以内具有超过1W个素数
注意输出,
犯错点:每行最多输出10个数字这种题,不可以多输出“换行”
def my_print(prime_list):
i=0
while(i+10<=len(prime_list)):
print(' '.join(prime_list[i:i+10]))
i+=10
if i<len(prime_list):
print(' '.join(prime_list[i:]))
n,m=map(int,input().split())
prime_index=[True]*200000
prime_index[0],prime_index[1]=False,False
prime_list=[]
#print("could")
for i in range(2,200000):
if prime_index[i]:
prime_list.append(str(i))
if len(prime_list)>10000:
#print("done")
break
for j in range(2,200000//i):
prime_index[i*j]=False
if n==m:
print(prime_list[n-1])
else:
#print(prime_list[:21])
prime_list=prime_list[n-1:m]
#print(prime_list)
my_print(prime_list)
NO.14 一遍过
字符串不能相减,只能通过ord换成ascii码再相减,而chr将ASCII换成字符
字典设置与re.match的熟练应用
import re
str_list=[]
for i in range(4):
str_list.append(input())
min_1=len(str_list[0]) if len(str_list[0])<=len(str_list[1]) else len(str_list[1])
min_2=len(str_list[2]) if len(str_list[2])<=len(str_list[3]) else len(str_list[3])
this_day=[]
pos=0
for i in range(min_1):
if str_list[0][i]==str_list[1][i] and re.match(r'[A-G]',str_list[1][i]):
this_day.append(str_list[0][i])
pos=i
break
for i in range(pos+1,min_1):
if str_list[0][i]==str_list[1][i] and re.match(r'[0-9A-N]',str_list[1][i]):
if re.match(r'[0-9]',str_list[1][i]):
this_day.append(str_list[0][i])
else:
this_day.append(ord(str_list[0][i])-ord('A')+10)
break
for i in range(min_2):
#print(str_list[2][i],str_list[3][i])
if str_list[2][i]==str_list[3][i] and re.match(r'[A-Za-z]',str_list[3][i]):
this_day.append(i)
week_dict={'A':"MON",'B':"TUE",'C':"WED",'D':"THU",'E':"FRI",'F':"SAT",'G':"SUN"}
print("{} {:0>2}:{:0>2}".format(week_dict[this_day[0]],this_day[1],this_day[2]))
NO.15 分类+排序
排序结果默认最小到大
- 单个list的排序,排序性最弱的放在最前面排序,排序性最强的放在最后面排序
- 可以先分成几个list最后拼接在一起,然后输出
def my_sorted(student_list):
student_list=sorted(student_list,key=lambda x:x[0])
student_list=sorted(student_list,key=lambda x:x[1],reverse=True)
student_list=sorted(student_list,key=lambda x:x[1]+x[2],reverse=True)
return student_list
num,l,h=map(int,input().split())
all_pass_student=[]
first_student,second_student,third_student,others=[],[],[],[]
for i in range(num):
one_student=[int(i) for i in input().split()]
if one_student[1]>=h and one_student[2]>=h:
first_student.append(one_student)
elif one_student[1]>=h and one_student[2]>=l:
second_student.append(one_student)
elif one_student[1]>=l and one_student[2]>=l and one_student[1]>=one_student[2]:
third_student.append(one_student)
elif one_student[1]>=l and one_student[2]>=l:
others.append(one_student)
else:
pass
all_pass_student=my_sorted(first_student)+my_sorted(second_student)+
my_sorted(third_student)+my_sorted(others)
print(len(all_pass_student))
for i in all_pass_student:
i=[str(ii) for ii in i]
print(' '.join(i))
NO.16 部分A+B简单部分
this_input = input()
a, da, b, db = this_input.split(' ')
pa, pb = "", ""
for i in a:
if i == da:
pa += i
for i in b:
if i == db:
pb += i
pb = '0' if pb == "" else pb
pa = '0' if pa == "" else pa
print(int(pa) + int(pb))
NO.17 更简单的题
a,b=map(int,input().split())
print("{} {}".format(a//b,a%b))
NO.18 ch_A,ch_B='B','B'一开始没有初始化好,导致两个测试点输出的是'',题目描述在不确定输出时要输出字母序小的,故没赢一次的要输出‘B’
n=int(input())
A_W,equal=0,0
dict_A_W,dict_Z_W={'B':0,'C':0,'J':0},{'B':0,'C':0,'J':0}
def this_A_W(A,B):
if A==B:
return 0
elif (A=='B' and B=='C') or (A=='C' and B=='J') or (A=='J' and B=='B'):
return 1
else:
return -1
for _ in range(n):
A,B=input().split()
cnt=this_A_W(A,B)
if cnt==1:
dict_A_W[A]+=1
A_W+=1
elif cnt==0:
equal+=1
else:
dict_Z_W[B]+=1
print("{} {} {}".format(A_W,equal,n-A_W-equal))
print("{} {} {}".format(n-A_W-equal,equal,A_W))
ch_A,ch_B='B','B'
max_A,max_B=0,0
for i in dict_A_W:
if dict_A_W[i]>max_A:
ch_A=i
max_A=dict_A_W[i]
if dict_Z_W[i]>max_B:
ch_B=i
max_B=dict_Z_W[i]
print('{} {}'.format(ch_A,ch_B))
NO.19 数字黑洞
要点:字符串填充0的函数zfill(宽度)
填充的时机,从一开始就要填充
center(宽度,“字符”)
ljust(宽度,“字符”)
rjust(宽度,“字符”)
start=input()
def anti_decrease(i):
ans=[]
for ii in i:
ans.append(ii)
return "".join(sorted(ans,reverse=True))
if start.count(start[0])==4:
print("{} - {} = 0000".format(start,start))
else:
start=str(start).zfill(4)
big_one=anti_decrease(start)
verse=big_one[::-1]
tem=int(big_one)-int(verse)
while tem!=6174:
print("{} - {} = {:0>4}".format(big_one,verse,tem))
tem=str(tem).zfill(4)
big_one=anti_decrease(tem)
verse=big_one[::-1]
tem=int(big_one)-int(verse)
print("{} - {} = 6174".format(big_one,verse))
NO.20
月饼 贪心 相关联的两个列表的排序
category,need=map(int,input().split())
list_weight=[float(i) for i in input().split()]
list_all_value=[float(i) for i in input().split()]
'''
list_value=[]
for i in range(category):
list_value.append(list_all_value/list_weight)
'''
t=zip(list_weight,list_all_value)
t=[tt for tt in t]
t=sorted(t,key=lambda x:x[1]/x[0],reverse=True)
weight=sum(list_weight)
index=0
benefits=0.0
while need>=0 and index<category:
if need>t[index][0]:
benefits+=t[index][1]
need-=t[index][0]
#print("here")
else:
benefits+=t[index][1]/t[index][0]*need
need=0
index+=1
print("{:.2f}".format(benefits))
网友评论