袁博特 2020-06-25 09:25 采纳率: 25%
浏览 105

请问,编译这个小程序时应该怎么输入什么呢?

#include
#include**

using namespace std;

int main()
{
vector ivec;
int ival;
while ( cin >> ival )
ivec.push_back( ival );

// we could have calculated the sum as we entered the
// values, but the idea is to iterate over the vector ...
int sum = 0;
for ( int  ix = 0; ix < ivec.size(); ++ix )
    sum += ivec[ ix ];
int average = sum / ivec.size();
cout << "Sum of " << ivec.size()
    << " elements: " << sum
    << ". Average: " << average << endl;

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-06-25 15:10
    关注

    while ( cin >> ival )
    这个意思是,输入任意个的整数,以0作为结束
    下面计算平均数和总和。

    问题解决的话,请点下采纳

    评论

报告相同问题?