#学习笔记

#用以练习python基础

#


原题链接:https://www.patest.cn/contests/pat-b-practise/1039


1039. 到底买不买(20)

小红想买些珠子做一串自己喜欢的珠串。卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖。于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有多少多余的珠子;如果不是,那么告诉她缺了多少珠子。

为方便起见,我们用[0-9]、[a-z]、[A-Z]范围内的字符来表示颜色。例如在图1中,第3串是小红想做的珠串;那么第1串可以买,因为包含了全部她想要的珠子,还多了8颗不需要的珠子;第2串不能买,因为没有黑色珠子,并且少了一颗红色的珠子。

gx_nkqhj1eeck3.jpg
图 1

输入格式:

每个输入包含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

1054. 求平均值 (20)

本题的基本要求非常简单:给定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


1012. 数字分类 (20)

给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:

  • A1 = 能被5整除的数字中所有偶数的和;

  • A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;

  • A3 = 被5除后余2的数字的个数;

  • A4 = 被5除后余3的数字的平均数,精确到小数点后1位;

  • A5 = 被5除后余4的数字中最大数字。

    输入格式:

    每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。

    输出格式:

    对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

    若其中某一类数字不存在,则在相应位置输出“N”。

    输入样例1:

    13 1 2 3 4 5 6 7 8 9 10 20 16 18

    输出样例1:

    30 11 2 9.7 9

    输入样例2:

    8 1 2 4 5 6 7 9 16

    输出样例2:

    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')