PAT 乙级 1012
Posted moujun1001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 乙级 1012相关的知识,希望对你有一定的参考价值。
最后一个测试点怎么也过不了,问题在于A2的判断,不能单纯地以0作为判断条件;假设满足A2条件的只有两个数6和6,计算结果仍然是0,但是输出A2的值是0不是‘N‘,因此需要给A2加一个计数变量作为判断条件
#include <iostream> #include <vector> #include <cstdio> using namespace std; int main() { vector<int> num; int n = 0; cin >> n; while (n--) { int tmp = 0; cin >> tmp; num.push_back(tmp); } int A1 = 0; int A2 = 0; int A3 = 0; float A4 = 0; int A5 = 0; int A2_cnt = 0; bool flag = true; int cnt = 0; int maxx = 0; for (int i = 0; i < num.size(); i++) { if (num[i] % 5 == 0 && num[i] % 2 == 0) A1 += num[i]; else if (num[i] % 5 == 1) { A2_cnt++; if (flag) { A2 += num[i]; flag = false; } else { A2 -= num[i]; flag = true; } } else if (num[i] % 5 == 2) A3++; else if (num[i] % 5 == 3) { A4 += num[i]; cnt++; } else if (num[i] % 5 == 4) { if (num[i] > maxx) maxx = num[i]; } } if (A4 != 0) A4 /= cnt; A5 = maxx; if (A1 == 0) cout << "N "; else cout << A1 << ‘ ‘; if (A2_cnt == 0) cout << "N "; else cout << A2 << ‘ ‘; if (A3 == 0) cout << "N "; else cout << A3 << ‘ ‘; if (A4 == 0) cout << "N "; else printf("%.1f ", A4); if (A5 == 0) cout << "N"; else cout << A5 << endl; return 0; }
以上是关于PAT 乙级 1012的主要内容,如果未能解决你的问题,请参考以下文章