PAT class b-1012 digital classification (20 points)

1012 digital classification (20 points)

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

A 1 = sum of all even numbers that can be divided by 5;
A ‚ 2 ‚ is the interleaved sum of the remaining 1 numbers divided by 5 in the given order, i.e. calculate n ‚ 1 ‚ n ‚ 2 ‚ n + n ‚ 3 ‚ n ‚ 4 ‚
A 3 = the number of the remaining 2 digits divided by 5;
A < 4 < = the average number of the remaining 3 digits divided by 5, accurate to 1 decimal place;
A 5 = the largest of the remaining 4 digits divided by 5.

Input format:

Each input contains 1 test case. Each test case first gives a positive integer N of no more than 1000, and then gives N positive integers of no more than 1000 to be classified. Numbers are separated by spaces.

Output format:

For the given N positive integers, A 1 ~ A 5 is calculated according to the requirements of the title and output in order 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, output N in the corresponding position.

Enter example 1:

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

Output example 1:

30 11 2 9.7 9

Enter example 2:

8 1 2 4 5 6 7 9 16

Output example 2:

N 11 2 N 9

#include<iostream>
#include<vector>
#include<cstdio>
using namespace std;
int main(){
 vector<int> number;
 vector<int> A1,A2,A3,A4,A5;
 int n,N,k=0;
 cin>>N;
 while(k<N){
  cin>>n;
  number.push_back(n);
  k++;
 }
 for(vector<int>::iterator it=number.begin();it<number.end();it++){
  if(*it % 5==0 &&*it%2==0)
   A1.push_back(*it);
  if(*it%5 == 1)
   A2.push_back(*it);
  if(*it%5 == 2)
   A3.push_back(*it);
  if(*it%5 == 3){
   A4.push_back(*it); 
  }
  if(*it%5 == 4)
   A5.push_back(*it);
 }
 int sum=0;  //sum storage and 
 for(vector<int>::iterator it = A1.begin();it<A1.end();it++)
  sum+=*it;
 int count =0,sum1=0; //count judge parity, sum1 store results 
 for(vector<int>::iterator it = A2.begin();it<A2.end();it++){
  if(count % 2==0){
   sum1+=*it;
  }
  else
   sum1-=*it;
  count++;
 }
 count=A3.size(); //Counter, count stores the number of elements in A3 
 double average=0; //Storage average 
 for(int i=0;i<A4.size();i++){
  average+=A4[i];
 }
 average = average/A4.size();
 int max = -1000; //Storage maximum 
 for(vector<int>::iterator it = A5.begin();it<A5.end();it++){
  if(*it>max)
   max = *it;
 }
 
 //Judge and output 
 if(A1.size() != 0)
  cout<<sum<<" ";
 else
  cout<<"N"<<" ";
 if(A2.size() != 0)
  cout<<sum1<<" ";
 else
  cout<<"N"<<" ";
 if(A3.size() != 0)
  cout<<count<<" ";
 else
  cout<<"N"<<" ";
 if(A4.size() != 0)
  printf("%.1lf ",average);
 else
  cout<<"N"<<" ";
 if(A5.size() != 0)
  cout<<max;
 else
  cout<<"N";
 return 0;
}

Summary: at the beginning, I didn't carefully examine the questions. I didn't pay attention to the number of input first. I introduced the vector and debugged it for an hour. The answer is always incorrect. You can use arrays directly, which is a little easier.

Posted by agadgil on Tue, 22 Oct 2019 08:39:42 -0700