【華爲OJ】記負均正

輸入:整數個數n  n個整數

輸出:整數中負數的個數 正整數的均數

<pre name="code" class="cpp">#include <iostream>  
#include<iomanip>
using namespace std;

int main()
{
	int n,nega=0,posi=0,total=0;
	cin >> n;
	int* ARR =new int[n];

	for (int i = 0;i < n;i++)
	{
		cin >> ARR[i];
		if (ARR[i] < 0)
			nega++;
		else if (ARR[i]>0)
		{
			posi++;
			total += ARR[i];
		}
	}
	cout << nega << ' ';
	if (posi > 0)
	{
		if (total%posi == 0)
		{
			int aver = total / posi;
			cout << nega << ' ' << aver << endl;
		}
		else
		{
			float aver = ((float)total) / posi;
			cout <<fixed<< setprecision(1) << aver << endl;

		}
	}
	else
		cout << 0;
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章