PAT Basic 1012 Digital Classification (20 points)

Posted by macastor on Tue, 30 Jul 2019 23:23:38 +0200

Given a series of positive integers, please categorize the numbers as required and output the following five numbers:

  • A 1 = the sum of all even numbers in a number divisible by 5;
  • A 2 = The number of the remaining 1 divided by 5 is interleaved in the given order, i.e. n 1 N 2 + n 3 N 4.
  • A 3 = Number of the remaining 2 digits divided by 5;
  • A. 4 = The average number of the remaining three digits divided by five, accurate to one decimal place;
  • A 5 = the largest number of the remaining 4 after being divided by 5.

Input format:

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

Output format:

For a given N positive integers, calculate A 1~A 5 according to the requirements of the title and output them sequentially in one line. Numbers are separated by spaces, but no extra spaces are allowed at the end of the line.

If one of the numbers does not exist, N is output at the corresponding position.

Input Sample 1:

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

Output Sample 1:

30 11 2 9.7 9

Input Sample 2:

8 1 2 4 5 6 7 9 16

Output Sample 2:

N 11 2 N 9


#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n,temp,res1=0,res2=0,res3=0,res5=-1;double res4=0;
    cin>>n;
    vector<int> a1,a2,a3,a4,a5;
    for(int i=0;i<n;i++){
        cin>>temp;
        switch(temp%5){
            case 0: if(temp%10==0) a1.push_back(temp);break;
            case 1: a2.push_back(temp);break;
            case 2: a3.push_back(temp);break;
            case 3: a4.push_back(temp);break;
            case 4: a5.push_back(temp);break;
        }
    }
    //Summation
    for(int i=0;i<a1.size();i++){
        res1+=a1[i];
    }
    //Staggered summation
    bool isPositive=true;//Positive and negative interlaced key
    for(int i=0;i<a2.size();i++){
        if(isPositive) res2+=a2[i];
        else res2-=a2[i];
        isPositive=!isPositive;
    }
    //count
    for(int i=0;i<a3.size();i++){
        res3++;
    }
    //Average
    for(int i=0;i<a4.size();i++){
        res4+=a4[i];
    }
    res4=res4/a4.size();
    //Maximum number
    for(int i=0;i<a5.size();i++){
        if(a5[i]>res5) res5=a5[i];
    }
    //output,It's more accurate to use blank judgment here, otherwise there will be a mistake.
    if(a1.size()==0) cout<<"N ";else cout<<res1<<" ";
    if(a2.size()==0) cout<<"N ";else cout<<res2<<" ";
    if(a3.size()==0) cout<<"N ";else cout<<res3<<" ";
    if(a4.size()==0) cout<<"N ";else printf("%.1f ",res4);
    if(a5.size()==0) cout<<"N";else cout<<res5;
    system("pause");
    return 0;
}

Topics: PHP