PAT (Basic Level) Practice 1012

1012. Number Classification(20)

time limit
100 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

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

A1 = sum of all even numbers in numbers divisible by 5; A2 = interleaved summation of numbers with remainder 1 after division by 5 in the given order, that is, calculate n1-n2+n3-n4...; A3 = be The number of digits with a remainder of 2 after division by 5; A4 = the average number of digits with a remainder of 3 after division by 5, accurate to 1 decimal place; A5 = the largest number of digits with a remainder of 4 after division 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

Analysis: The subject itself is not difficult, but it is more troublesome. Here, in order to practice the use of vector, we choose to use vector to write. The input number is filtered and stuffed into the vector, and the operation is performed as required. Finally, judge whether the output 'N' is the number of numbers according to the conditions.

Code:

#include<iostream>
#include<vector>
#include<iomanip>
using namespace std;
intmain()
{
	cout.precision(1);
	vector<int> A1, A2, A3, A4, A5;
	int a1 = 0, a2 = 0, a3 = 0, a5 = 0;
	double a4 = 0.0;
	int n;
	int temp;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> temp;
		if(temp % 5 == 0){
			A1.push_back(temp);
		}else if(temp % 5 == 1){
			A2.push_back(temp);
		}else if(temp % 5 == 2){
			A3.push_back(temp);
		}else if(temp % 5 == 3){
			A4.push_back(temp);
		}else if(temp % 5 == 4){
			A5.push_back(temp);
		}
	}
	int cnt = 0;
	for(vector<int>::iterator it = A1.begin(); it != A1.end(); it++){
		if(*it % 2 == 0){
			a1 += *it;
			cnt++;
		}
	}
	int flag = 1;
	for(vector<int>::iterator it = A2.begin(); it != A2.end(); it++){
		a2 += flag * (*it);
		flag *= -1;
	}
	a3 = A3.size();
	for(vector<int>::iterator it = A4.begin(); it != A4.end(); it++){
		a4 = a4 + (double)(*it) / A4.size();
	}
	int maxN = -9999;
	for(vector<int>::iterator it = A5.begin(); it != A5.end(); it++){
		if((*it) > maxN){
			maxN = (*it);
		}
	}
	a5 = maxN;
	if(A1.size() == 0 || cnt == 0){
		cout << "N ";
	}else{
		cout << a1 << ' ';
	}
	if(A2.size() == 0){
		cout << "N ";
	}else{
		cout << a2 << ' ';
	}
	if(A3.size() == 0){
		cout << "N ";
	}else{
		cout << a3 << ' ';
	}
	if(A4.size() == 0){
		cout << "N ";
	}else{
		cout  << fixed << a4 << ' ';
	}
	if(A5.size() == 0){
		cout << "N" << endl;
	}else{
		cout << a5 << endl;
	}
	return 0;
}

Guess you like

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