题解 | # 华为机试 HJ105 记负均正II#
记负均正II
https://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main() {
int num;
vector<int> vec1;
vector<int> negVec2;
while (cin >> num) { // 注意 while 处理多个 case
if (num >= 0) {
vec1.push_back(num);
} else {
negVec2.push_back(num);
}
}
cout << static_cast<int>(negVec2.size()) << std::endl;
double sum = 0.0;
double average = 0.0;
for (size_t i = 0; i < vec1.size(); i++) {
sum += vec1[i];
}
int count = static_cast<int>(vec1.size());
if (count == 0) {
average = 0;
} else {
average = sum / count;
}
std::cout << setprecision(1) << fixed << average << endl;
}