给定一系列正整数,请按要求对数字进行分类,并输出以下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
//注意不能用0判断A2的个数 #include <iostream> #include <algorithm> #include <iomanip> using namespace std; int ans[5] = { 0 }; int cntA2 = 0; int cntA4 = 0; bool a2 = false; int main(){ int n; cin >> n; int num; while (n--){ cin >> num; if (num % 5 == 0){ if (num % 2 == 0){ ans[0] += num; } } else if (num % 5 == 1){ if (cntA2 % 2 == 0){ ans[1] += num; } else{ ans[1] -= num; } cntA2++; } else if (num % 5 == 2){ ans[2] ++; } else if (num % 5 == 3){ ans[3] += num; cntA4++; } else if (num % 5 == 4){ if (num > ans[4]){ ans[4] = num; } } } for (int i = 0; i < 5; i++){ if (i == 3){ if (ans[3] == 0){ cout << "N "; } else{ cout << fixed << setprecision(1) << (double)ans[3] / cntA4 <<" "; } } else if (i == 4){ if (ans[i] == 0){ cout << "N"; } else { cout << ans[i]; } } else if (i == 1){ if (cntA2){ cout << ans[i] << " "; } else{ cout << "N "; } } else{ if (ans[i] == 0){ cout << "N "; } else{ cout << ans[i] << " "; } } } system("pause"); return 0; }