华为机试 HJ105题解 | #记负均正II#
记负均正II
https://www.nowcoder.com/practice/64f6f222499c4c94b338e588592b6a62
#include <iomanip> #include <iostream> #include <vector> // #include <iomanip> #include<bits/stdc++.h> using namespace std; int main() { vector<int> nums; int n; while (scanf("%d", &n) != EOF) { nums.push_back(n); } double average = 0.0; double totalSum = 0.0; int degCnt = 0; // 正数和0的个数 int negCnt = 0; // 负数个数 for (auto num : nums) { if (num < 0) { negCnt++; } else { degCnt++; totalSum += num; } } average = totalSum / degCnt; std::cout << negCnt << std::endl; if (degCnt == 0) { std::cout << "0.0" << std::endl; } else { std::cout << setprecision(1) << fixed << average << std::endl; } return 0; } // 64 位输出请用 printf("%lld")