PAT digital classification Python version

Given to a series of positive integers, press the demand for digital classification, and outputs the following five numbers:

A1 = the number can be divisible by 5 and all even;

A2 = 5 remainders after division will be the number 1 is given in a staggered sequence summing, i.e. n1-n2 + n3-n4 ... calculated;

A3 = the number is more than 5 after the addition of the number 2;

A4 = Mean remainders after division by 3 numbers 5, accurate to one decimal place;

A5 = 5 is the maximum digital number other than 4.

Input formats:

Each input comprises a test. Each test case is given a first positive integer of not more than 1000 N, and then gives the N does not exceed 1000 to be classified positive integer. Between numbers separated by a space.

Output formats:

For a given  N positive integers, is calculated by the subject of the request  A . 1 ~ A . 5 and the output order in a row. Between numbers separated by spaces, but the end of the line may not have the extra space. If a certain type wherein the number does not exist, the output in the corresponding position  N.

Sample Input 1:

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

Output Sample 1:

30 11 2 9.7 9

Sample Input 2:

8 1 2 4 5 6 7 9 16

Output Sample 2:

N 11 2 N 9
a=list(map(int,input().split()[1:]))
a1=0
a2=0
k=1
a3=0
sum=0
count=0
b=[]
l=0
age,m=0,0
for i in range(len(a)):
    if a[i]%5==0 and a[i]%2==0:
        a1+=a[i]
    elif a[i]%5==1:
        a2+=a[i]*k
        k=-k
        l+=1
    elif a[i]%5==2:
        a3+=1
    elif a[i]%5==3:
        sum+=a[i]
        count+=1
    else:
        if a[i]%5:
            b.append(a[i])
if count:
    age=round(sum/count,1)
if b:
    m=max(b)
def printf(t):
    if t:
        print(t,end = " ")
    else:
        print('N',end = " ")
printf(a1)
if l:
    print(a2,end = " ")
else:
    print('N',end = " ")
printf(a3)
printf(age)
if m:
    print(m)
else:
    print('N')

This subject will be difficult, according to the requirements carefully divided into many categories is OK, however, if such figures A4 If not, then directly calculate the average division with a division by zero exception occurs, the test point on the pass, so, before doing the division, there is no need to determine what such figures; and if the Python max () function to seek a maximum value in the sequence, then this sequence can not be empty, therefore, need to make a judgment, on the other do not have much left.

There is also a small episode, I was in a cow brush off network problems, and then I found them a test point seems to have problems:

According to the requirements of the subject, if a certain type of numbers do not exist, the corresponding output "N", and that for this type of digital A2, should it not, only the output "N", but he has and is 0 (there is such a kind case, because the subject did not enter duplicate provisions of positive integers), the output should be 0, for example, such a simple test case:

511 222 
in accordance with the subject of the request should be output: N 0 3 NN is correct, output: NN 3 NN is wrong, but I tried it, it seems that in both cases the output can be AC
because they are relatively dish, so afraid of their own analysis was wrong, I went to the PTA platform tried it, did not think, I guess turned out to be right, then I have submitted this question of pilot error correction for visual inspection of the cattle off the net, quite happy

Guess you like

Origin www.cnblogs.com/andrew3/p/12623217.html