PAT Level B | 1012 Number Classification (20 points)

Given a series of positive integers, please classify the numbers as required, and output the following 5 numbers:
A 1 = the sum of all even numbers in the numbers divisible by 5;
A 2 = the number with 1 remaining after being divided by 5 summing interleaved order given, i.e. calculated. 1 -n 2 + n-n--n. 4. 3 ⋯;
a . 3 = 2 after the number is more than the number of other 5 ;
A 4 = the average number of the remaining 3 digits after dividing by 5, accurate to 1 decimal place;
A 5 = the largest digit of the remaining 4 digits after dividing by 5.

Input format:

Each input contains 1 test case. Each test case first gives a positive integer N not exceeding 1000, and then N positive integers not exceeding 1000 to be classified. The numbers are separated by spaces.

Output format:

For the given N positive integers, calculate A 1 ~ A 5 according to the requirements of the question and output them in order on a line. The numbers are separated by spaces, but there must be no extra spaces at the end of the line. If a certain type of number does not exist, output N at the corresponding position.

Input example 1:

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

Sample output 1:

30 11 2 9.7 9

Input example 2:

8 1 2 4 5 6 7 9 16

Output sample 2:

N 11 2 N 9

Idea: Pay attention to the classification, take it slowly, and then there is a trap in this question that A2 may be 0!

#include <iostream>
using namespace std;

int main()
{
    
    
    int n,a[5]={
    
    0},tmp,flag1=1,flag2=1,count=0,max=0;
    cin >>n;
    for(int i=0;i<n;i++){
    
    
        scanf("%d",&tmp);
        if(tmp%5==0&&tmp%2==0)
            a[0]+=tmp;
        else if(tmp%5==1){
    
    
            flag2=0;
            a[1]+=flag1*tmp;
            flag1=-flag1;
        }
        else if(tmp%5==2)
            a[2]++;
        else if(tmp%5==3){
    
    
            a[3]+=tmp;
            count++;
        }
        else if(tmp%5==4)
            if(max<tmp){
    
    
                max=tmp;
                a[4]=max;
            }
    }
    if(a[0]==0)
        printf("N");
    else
        printf("%d",a[0]);
    for(int i=1;i<5;i++){
    
    
        if(a[i]==0){
    
    
            if(i==1){
    
    
                if(!flag2)
                    cout <<" 0";
                else
                    cout <<" N";
                continue;
            }
            printf(" N");
        }
        else{
    
    
            if(i==3){
    
    
                printf(" %.1lf",(double)a[3]*1.0/count);
                continue;
            }
            printf(" %d",a[i]);
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44888152/article/details/108628339