我遇到了一个问题,我必须取一行数字之和的平均值,以零结束,不能计算在等式中.然而,虽然我的第一个输出值非常接近或有时是正确的,但我的其他值完全没有了.如果有人能弄明白我的代码错误,我会很高兴.
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int a, b, sum, average;
int numb = 0;
cin>>a;
for (int i=0;i<a;i++) {
do {
cin>>b;
sum= b+sum;
numb++;
} while (b!=0);
average = sum/(numb-1);
cout<<round(average)<<" ";
}
}
Run Code Online (Sandbox Code Playgroud)
输入:
11
780 1610 565 799 1664 431 0
12848 10728 4091 12286 8035 0
959 418 171 255 694 78 393 917 119 1016 929 761 363 0
14930 11543 11508 3062 1545 8434 6504 2631 0
418 …
c++ ×1