YTU 2451: 股市风云

版权声明:转载请附上原文链接哟! https://blog.csdn.net/weixin_44170305/article/details/90174379

不恋尘世浮华,不写红尘纷扰,不叹世道苍凉,不惹情思哀怨,闲看花开,静待花落,冷暖自知,干净如始。

题目描述

股市强烈动荡,有涨有跌。现在有一组数据表示各公司的涨跌(涨为正,跌为负,不动为零),要求统计出平均涨幅和平均跌幅。

输入

一组数,其中有正数,也有负数,还有0。输入的个数不定,另外,不会出现只有正数或只有负数的情况。

输出

第一行输出见涨的数目和遇跌的数目;

第二行输出平均涨幅(正数的平均数)和平均跌幅(负数的平均数,再取反),保留小数点后3位。

样例输入

copy

<span style="color:#333333">5 0 -1 1.5 2.3 -0.3 2.4 0 7.9 -4.3</span>

样例输出

<span style="color:#333333">5 3
3.820 1.867</span>

提示

 

(1)用于处理不定数目的输入,参考:

int main()

{

    int a,b;

    while(cin >>a)

    {

        cout << a << endl;

    }

    return 0;

}

(2)输出x的值,保留两位小数,用:

cout<<setiosflags(ios::fixed)<<setprecision(3)<<x<<endl;

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<iomanip>
#include<queue>
#include<set>
#include<stack>
using namespace std;
int main()
{
    double q;
    double sum1=0.0,sum2=0.0;
    int t1=0,t2=0;
    while(cin>>q)
    {
        if(q>0)
        {
            t1++;
            sum1+=q;
        }
        if(q<0)
        {
            t2++;
            sum2+=q;
        }
        if(getchar()=='\n')
            break;
    }
    double ave1,ave2;
    ave1=sum1/t1;
    ave2=sum2/t2;
    cout<<t1<<" "<<t2<<endl;
    cout<<setiosflags(ios::fixed)<<setprecision(3)<<ave1<<' '<<(-ave2)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/90174379