#学习笔记
#用以练习 python 基础
#
原题链接: https://www.patest.cn/contests/pat-b-practise/1039
小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。
为方便起见,我们用 [0-9]、[a-z]、[A-Z] 范围内的字符来表示颜色。例如在图 1 中,第 3 串是小红想做的珠串;那么第 1 串可以买,因为包含了全部她想要的珠子,还多了 8 颗不需要的珠子;第 2 串不能买,因为没有黑色珠子,并且少了一颗红色的珠子。
输入格式:
每个输入包含 1 个测试用例。每个测试用例分别在 2 行中先后给出摊主的珠串和小红想做的珠串,两串都不超过 1000 个珠子。
输出格式:
如果可以买,则在一行中输出 "Yes" 以及有多少多余的珠子;如果不可以买,则在一行中输出 "No" 以及缺了多少珠子。其间以 1 个空格分隔。
输入样例 1:
- ppRYYGrrYBR2258
- YrR8RrY
输出样例 1:
- Yes 8
输入样例 2:
- ppRYYGrrYB225
- YrR8RrY
输出样例 2:
- No 2
AC 代码
- L=list(input())
- N=list(input())
- x=len(N)
- t=-1
- for i in range(len(N)):
- try:
- if L.index(N[t])>=0:
- L.pop(L.index(N[t]))
- x=x-1
- N.pop()
- except:
- t=t-1
- if len(N)==0:
- print("Yes",len(L))
- else:
- print("No",x)
原题链接:https://www.patest.cn/contests/pat-b-practise/1054
本题的基本要求非常简单:给定 N 个实数,计算它们的平均值。但复杂的是有些输入数据可能是非法的。一个 "合法" 的输入是 [-1000,1000] 区间内的实数,并且最多精确到小数点后 2 位。当你计算平均值的时候,不能把那些非法的数据算在内。
输入格式:
输入第一行给出正整数 N(<=100)。随后一行给出 N 个实数,数字间以一个空格分隔。
输出格式:
对每个非法输入,在一行中输出 "ERROR: X is not a legal number",其中 X 是输入。最后在一行中输出结果:"The average of K numbers is Y",其中 K 是合法输入的个数,Y 是它们的平均值,精确到小数点后 2 位。如果平均值无法计算,则用 "Undefined" 替换 Y。如果 K 为 1,则输出 "The average of 1 number is Y"。
输入样例 1:
- 7
- 5 -3.2 aaa 9999 2.3.4 7.123 2.35
输出样例 1:
- ERROR: aaa is not a legal number
- ERROR: 9999 is not a legal number
- ERROR: 2.3.4 is not a legal number
- ERROR: 7.123 is not a legal number
- The average of 3 numbers is 1.38
输入样例 2:
- 2
- aaa -9999
输出样例 2:
- ERROR: aaa is not a legal number
- ERROR: -9999 is not a legal number
- The average of 0 numbers is Undefined
AC 代码
- N=int(input())
- L=list(str(input()).split(' '))
- sum1=0
- x=0
- for i in range(N):
- try:
- if float(L[i])>=-1000 and float(L[i])<=1000 and len(str(float(L[i])).split('.')[1]) <=2:
- sum1=sum1+float(L[i])
- x=x+1
- else:
- print('ERROR:',L[i],'is not a legal number')
- except:
- print('ERROR:',L[i],'is not a legal number')
- if x==0:
- print('The average of 0 numbers is Undefined')
- elif x==1:
- print('The average of 1 number is %0.2f' % (sum1))
- else:
- print('The average of %d numbers is %.2f' % (x,sum1/x))
原题链接:https://www.patest.cn/contests/pat-b-practise/1012
给定一系列正整数,请按要求对数字进行分类,并输出以下 5 个数字:
输出样例 1:
- 13 1 2 3 4 5 6 7 8 9 10 20 16 18
输入样例 2:
- 30 11 2 9.7 9
输出样例 2:
- 8 1 2 4 5 6 7 9 16
- N 11 2 N 9
- import math
- L=list(str(input()).split(' '))
- L.pop(0)
- A1=[]
- A3=[]
- A4=[]
- A5=[]
- sum1=0
- k=0
- max1=-1
- q=0
- try:
- for i in L:
- if i.isalnum()==True :
- if int(i)%5==0 and int(i) % 2 ==0:
- A1.append(int(i))
- if int(i)%5==1 :
- sum1=int(sum1+int(i)*math.pow(-1,k))
- k=k+1
- if int(i) % 5 == 2 :
- A3.append(i)
- if int(i) % 5 == 3:
- A4.append(int(i))
- if int(i) % 5 == 4:
- if int(i) > max1:
- max1=int(i)
- if len(A1) ==0:
- A5.append('N')
- else:
- A5.append(sum(A1))
- if k ==0:
- A5.append('N')
- else:
- A5.append(sum1)
- if len(A3) ==0:
- A5.append('N')
- else:
- A5.append(len(A3))
- if len(A4) ==0:
- A5.append('N')
- else:
- A5.append(round(sum(A4)/len(A4),1))
- if max1==-1:
- A5.append('N')
- else:
- A5.append(max1)
- print(A5[0],A5[1],A5[2],A5[3],A5[4])
- except:
- print('N N N N N')
来源: http://www.bubuko.com/infodetail-2043059.html