PAT Grade B 1012 Digital Classification (20 points)

topic content

Given a series of positive integers, sort the numbers as required and output the following 5 numbers:

  • A1​ = sum of all even numbers divisible by 5;
  • A2​ = Interleave the sum of the numbers with 1 remaining after division by 5 in the given order, i.e. calculate n1​−n2​+n3​−n4​…;
  • A3​ = the number of digits with 2 remaining after division by 5;
  • A4​ = the average of the digits with the remainder 3 after division by 5, accurate to 1 decimal place;
  • A5​ = the largest number of numbers that have a remainder of 4 when divided by 5.

Input format:

Each input contains 1 test case. Each test case is given a positive integer N up to 1000, followed by N positive integers up to 1000 to be classified. The numbers are separated by spaces.

Output format:

For the given N positive integers, calculate A1​~A5​ as required by the question and output them sequentially in one line. Numbers are separated by spaces, but there must be no extra spaces at the end of the line.

If one of the types of numbers does not exist, it will be output in the corresponding position  N.

Input sample 1:

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

no blank line at the end

Sample output 1:

30 11 2 9.7 9

no blank line at the end

Input sample 2:

8 1 2 4 5 6 7 9 16

no blank line at the end

Sample output 2:

N 11 2 N 9

no blank line at the end

Problem solving ideas

The data range of N given in the title is 1000, so we can open an array larger than 1000 and use it, but in order to increase the difficulty, I chose another implementation method, which is to use malloc to create a dynamic array to implement. Design 5 functions to reduce the coupling between codes. These 5 functions correspond to the 5 requirements in the title respectively. For details, see the code

Detailed code

#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
int is_a[5] = {0};
int N;
int a1(int a[]){
    int sum = 0,count = 0;
    for(int i = 0;i<N;i++){
        if(a[i]%5==0&&a[i]%2==0) {
            sum+=a[i];
        count++;
        }
    }
    if(count == 0) is_a[0] = 1;
    return sum;
}
int a2(int a[]){
    int sum = 0,p = 1,count = 0;
    for(int i = 0;i<N;i++){
        if(a[i]%5==1) {
            sum+=a[i]*p;
            p = -p;
            count++;
    }
    }
        if(count == 0) is_a[1] = 1;
        return sum;

}
int a3(int a[]){
    int count = 0;
    for(int i = 0;i<N;i++){
        if(a[i]%5==2) {
            count++;
    }
    }
    if(count == 0) is_a[2] = 1;
    return count;
    
}
float a4(int a[]){
    float sum = 0,count = 0;
    for(int i = 0;i<N;i++){
        if(a[i]%5==3) {
            sum+=a[i];
            count++;
    }
    }
     if(count == 0) is_a[3] = 1;
    return sum/count;
    
}
int a5(int a[]){
    int max = 0,count = 0;
     for(int i = 0;i<N;i++){
        if(a[i]%5==4) {
            if(a[i]>max) max = a[i];
            count++;
    }
    }
    if(count == 0) is_a[4] = 1;
    return max;
}
int main(){
    int n,*a;
    cin>>n;
    N = n;
    a = (int *)malloc(n*sizeof(int));
    for(int i = 0;i<n;i++){
        cin>>a[i];
    }
    a1(a);a2(a);a3(a);a4(a);a5(a);
        if(is_a[0]==1) cout<<"N ";
        else cout<<a1(a)<<" ";
    if(is_a[1]==1) cout<<"N ";
        else cout<<a2(a)<<" ";
    if(is_a[2]==1) cout<<"N ";
        else cout<<a3(a)<<" ";
    if(is_a[3]==1) cout<<"N ";
        else printf("%.1f ",a4(a));
    if(is_a[4]==1) cout<<"N";
        else cout<<a5(a);
    
}

Guess you like

Origin blog.csdn.net/weixin_45660485/article/details/119286773