问题描述
输入格式:
每个输入包含 1 个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N,随后给出 N 个不超过 1000 的待分类的正整数。数字间以空格分隔。
输出格式:
对给定的 N 个正整数,按题目要求计算 A1 ~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。
若其中某一类数字不存在,则在相应位置输出 N。
输入样例 1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
输出样例 1:
30 11 2 9.7 9
输入样例 2:
8 1 2 4 5 6 7 9 16
输出样例 2:
N 11 2 N 9
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
int num;
int a1 = 0;
bool f1 = false;
int a2 = 0;
bool f2 = false;
int a3 = 0;
bool f3 = false;
double a4 = 0;
bool f4 = false;
int a5 = 0;
bool f5 = false;
int count = 0;
bool flag = true;
for (int i = 0; i < n; i++) {
cin >> num;
if (num % 5 == 0 && num % 2 == 0) {
a1 += num;
f1 = true;
}
else if (num % 5 == 1) {
f2 = true;
if (flag) {
a2 += num;
flag = !flag;
}
else {
a2 -= num;
flag = !flag;
}
}
else if (num % 5 == 2) {
f3 = true;
a3++;
}
else if (num % 5 == 3) {
f4 = true;
a4 += num;
count++;
}
else if(num % 5 == 4){
f5 = true;
if (a5 < num) {
a5 = num;
}
}
}
if (f1) {
cout << a1 << " ";
}
else {
cout << 'N' << " ";
}
if (f2) {
cout << a2 << " ";
}
else {
cout << 'N' << " ";
}
if (f3) {
cout << a3 << " ";
}
else {
cout << 'N' << " ";
}
if (f4) {
printf("%0.1f ", a4 / count);
}
else {
cout << 'N' << " ";
}
if (f5) {
cout << a5;
}
else {
cout << 'N';
}
return 0;
}