给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
- A1 = 能被5整除的数字中所有偶数的和;
- A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4…;
- A3 = 被5除后余2的数字的个数;
- A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
- A5 = 被5除后余4的数字中最大数字。
输入格式:
每个输入包含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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include <iostream> #include <cstdio>
using namespace std;
void getA1(int *number, int n) { int sum = 0; bool isEnter = false; for (int i = 0; i < n; i++) { if (number[i] % 5 == 0 && number[i] % 2 == 0) { sum += number[i]; isEnter = true; } }
if (!isEnter) { cout << "N "; } else { cout << sum << " "; } }
void getA2(int *number, int n) { int sign = 1; int sum = 0; bool isEnter = false; for (int i = 0; i < n; i++) { if (number[i] % 5 == 1) { sum += sign * number[i]; sign = -sign; isEnter = true; } }
if (isEnter == false) { cout << "N "; } else { cout << sum << " "; } }
void getA3(int *number, int n) { int cnt = 0; for (int i = 0; i < n; i++) { if (number[i] % 5 == 2) { cnt++; } }
if (cnt == 0) { cout << "N "; } else { cout << cnt << " "; } }
void getA4(int *number, int n) { int cnt = 0, sum = 0; for (int i = 0; i < n; i++) { if (number[i] % 5 == 3) { sum += number[i]; cnt++; } }
if (cnt == 0) { cout << "N "; } else { printf("%.1f ", sum * 1.0 / cnt); } }
void getA5(int *number, int n) { int max = 0; for (int i = 0; i < n; i++) { if (number[i] % 5 == 4) { if (max < number[i]) { max = number[i]; } } }
if (max == 0) { cout << "N"; } else { cout << max; } }
int main() { int n; cin >> n;
int *number = new int[10001]; for (int i = 0; i < n; i++) { cin >> number[i]; }
getA1(number, n); getA2(number, n); getA3(number, n); getA4(number, n); getA5(number, n);
delete[] number; }
|