/*
测试是否肥胖
*/
#include <iostream>
using namespace std;
int main()
{
int h,d;
double g;
cout << "请输入身高(cm):" ;
cin>>h;
cout<<"请输入体重(kg):";
cin>>g;
d=h-100;
if(g<d*0.8)
cout<<"low,请注意加强营养!"<<endl;
else if(g>d*1.2)
cout<<"high,请注意控制饮食!"<<endl;
else
cout<<"normal,请注意保持!"<<endl;
return 0;
}股市强烈动荡,有涨有跌。现在有一组数据表示各公司的涨跌(涨为正,跌为负,不动为零),要求统计出平均涨幅和平均跌幅。Input
一组数,其中有正数,也有负数,还有0。输入的个数不定,另外,不会出现只有正数或只有负数的情况。
Output
第一行输出见涨的数目和遇跌的数目;
第二行输出平均涨幅(正数的平均数)和平均跌幅(负数的平均数,再取反),保留小数点后3位。
Sample Input
5 0 -1 1.5 2.3 -0.3 2.4 0 7.9 -4.3
Sample Output
5 3
3.820 1.867
HINT
(1)用于处理不定数目的输入,参考:
int main()
{
int a,b;
while(cin >>a)
{
cout << a << endl;
}
return 0;
}
(2)输出x的值,保留两位小数,用:
cout<<setiosflags(ios::fixed)<<setprecision(3)<<x<<endl;
参考解答:(在调试时,最后一个数输完按CTRL+Z)
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
int i=0,j=0;
float a,s=0,t=0;
while(cin>>a)
{
if(a>0)
{
i+=1;
s+=a;
}
else if(a<0)
{
j+=1;
t+=a;
}
}
cout << "见涨的股票有"<<i <<"支,见跌的股票有"<<j<<"支。"<< endl;
cout<<"平均涨幅为"<<setiosflags(ios::fixed)<<setprecision(3)<<s/i<<",平均跌幅为"<<(-t/j)<<endl;
return 0;
}
954

被折叠的 条评论
为什么被折叠?



