华为机试 计负均正

来源:互联网 发布:淘宝客服在家做 编辑:程序博客网 时间:2024/05/21 22:43

来源:牛客网 计负均正

题目描述

首先输入要输入的整数个数n,然后输入n个整数。输出为n个整数中负数的个数,和所有正整数的平均值,结果保留一位小数。

输入描述:

首先输入一个正整数n,然后输入n个整数。

输出描述:

输出负数的个数,和所有正整数的平均值。

输入例子:

5
1 2 3 4 5

输出例子:

0 3

参考代码

#include <iostream>#include <stdio.h>using namespace std;int main(){    int n,x;    int cnt=0,sum=0,cnt2;    while (cin>>n){        sum=0;        cnt=0;        cnt2=0;        for (int i=0;i<n;i++){            cin>>x;            if (x<0) cnt++;            if (x>0){                cnt2++;                sum+=x;            }        }        if (sum%cnt2==0)            printf("%d %d\n",cnt,sum/cnt2);        else            printf("%d %.1f\n",cnt,sum*1.0/cnt2);    }    return 0;}
0 0
原创粉丝点击