题解 | #记负均正#

记负均正

https://www.nowcoder.com/practice/6abde6ffcc354ea1a8333836bd6876b8

#include <iostream>
#include <iomanip>
using namespace std;

void stats(const int n) {
    int negamount = 0;//负数个数
    //为避免得到整数,将累加器设为浮点类型
    float posisum = 0;//正数总和
    int posiamount = 0;//正数个数
    for (int i = 0; i < n; i++) {
        int val = 0;//记录输入数值
        cin >> val;
        //如果是负数,记一个负数
        if (val < 0)
            negamount++;
        //如果是正数,进行累加,并记数
        if (val > 0) {
            posiamount++;
            posisum += val;
        }
        //如果为0,不进行操作
    }
    //输出
    cout << negamount << " ";
    if (posiamount == 0)
        cout << "0.0";
    else
        cout << fixed << setprecision(1) << static_cast<double>(posisum / posiamount);
    cout << endl;
    return;
}

int main() {
    int n = 0;
    while (cin >> n) { // 注意 while 处理多个 case
        stats(n);
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务