首页 > 编程 > C++ > 正文

[华为OJ--C++]064-记负均正II

2019-11-08 01:30:52
字体:
来源:转载
供稿:网友

题目描述:从输入任意个整型数,统计其中的负数个数并求所有非负数的平均值

输入描述:输入任意个整数,以ctr+z键回车结束输入

输出描述:输出负数个数以及所有非负数的平均值

输入例子:

-13

-4

-7

^V

输出例子:

3

0.0

算法实现: 

#include<iostream>    #include<vector>  #include<iomanip>  using namespace std;        //************************************************      // * Author: 赵志乾      // * Date: 2017-2-21       // * Declaration: All Rigths Reserved !!!      //***********************************************/        int main()    {       	int nnum=0,pnum=0;      int sum=0,data;	while(cin>>data)	{		if(data<0)			nnum++;		else		{			sum+=data;			pnum++;		}	}   	cout<<nnum<<endl;	if(pnum!=0)	{		double ret=1.0*sum/pnum;          cout.setf(ios::fixed);           cout.PRecision(1);          cout<<ret<<endl; 	}	else	{		cout.setf(ios::fixed);           cout.precision(1);  		cout<<0.0<<endl;	}      return 0;    }    


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选