C++·PAT Grade B 1012. Digital Classification (20/20)

/*
1012. Number Classification(20)

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

A1 = the sum of all even numbers in numbers divisible by 5;
A2 = Interleave the sum of the numbers that are divided by 5 and have 1 remaining in the given order, that is, calculate n1-n2+n3-n4...;
A3 = the number of digits with 2 remaining after dividing by 5;
A4 = the average of the numbers with the remainder 3 after dividing 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 first gives a positive integer N not exceeding 1000, and then gives N positive integers not exceeding 1000 to be classified. The numbers are separated by spaces.
Output format:
    For the given N positive integers, calculate A1~A5 according to the requirements of 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, output "N" in the corresponding position.

Input sample 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 sample 2:
    8 1 2 4 5 6 7 9 16
Sample output 2:
    N 11 2 N 9
*/
/*
    Ideas:
        1. Enter data in sequence
            1.1 According to the modulus value, determine which type of data the use case array element belongs to
            1.2 Preprocessing classified data according to classification criteria
        2. Output according to the specified format.

    Notice:
        The boundary conditions are details, such as: Counting A2: judging whether there is an element of A2, to avoid ambiguity when the summation result is 0.
*/
#include <iostream>

using namespace std;

intmain()
{
    int size;
    int tmp, mod, A2_count = 0, A2_prev = 1, A4_count = 0;//tmp: Temporarily store the original data element; A2_prev: The sign of the previous A2 element, the default is +, 1: +, -1 :-; A3_count: Count the number of elements in A3
    int A1 = 0, A2 = 0, A3 = 0, A5 = -1;//A5: Store the largest number in the number that remains 4 after dividing by 5
    double A4 = 0;//A4: store the sum of elements, and finally average
    scanf("%d", &size);
    while(size--){
        scanf("%d", &tmp);
        mod = tmp % 5;
        if(mod == 0){
            if(tmp % 2 == 0){
                A1 += tmp;//A1: the sum of all even numbers in the numbers divisible by 5
            }
        }else if(mod == 1){//A2: Interleave and sum the numbers with the remainder of 1 divided by 5 in the given order, that is, calculate n1-n2+n3-n4...;
            A2 += A2_prev * tmp;
            A2_prev *= -1;
            A2_count++;//Note: The reason for counting A2: to determine whether there is an element of A2, to avoid ambiguity when the summation result is 0.
        } else if(mod == 2) {//A3: The number of numbers remaining 2 after being divided by 5;
            A3 ++;
        } else if(mod == 3){//A4: The average number of the remaining 3 numbers after being divided by 5, accurate to 1 decimal place;
            A4 += tmp;//cumulative summation
            A4_count++;//Count
        } else {//A5: The largest number in the number that remains 4 after being divided by 5.
            A5 = tmp > A5 ? tmp : A5;
        }
    }

    if(A1 != 0){
        printf("%d ", A1);
    } else {
        printf("%s ", "N");
    }
    if(A2_count != 0){
        printf("%d ", A2);
    } else {
        printf("%s ", "N");
    }
    if(A3 != 0){
        printf("%d ", A3);
    } else {
        printf("%s ", "N");
    }
    if(A4 != 0){
        printf("%.1f ", A4 / A4_count);
    } else {
        printf("%s ", "N");
    }
    if(A5 != -1){
        printf("%d", A5);
    } else {
        printf("%s", "N");
    }
//    printf("%d %d %d %.1f %d", A1, A2, A3, A4 / A4_count, A5);//test

    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324888929&siteId=291194637