我可以计算这些数字的平均值吗?

编程入门 行业动态 更新时间:2024-10-25 00:33:58
本文介绍了我可以计算这些数字的平均值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否可以计算一些数字的平均值:

I was wondering if it's possible to calculate the average of some numbers if I have this:

int currentCount = 12; float currentScore = 6.1123 (this is a range of 1 <-> 10).

现在,如果我得到另一个分数(比如4.5),我可以重新计算平均值了吗?

Now, if I receive another score (let's say 4.5), can I recalculate the average so it would be something like:

int currentCount now equals 13 float currentScore now equals ?????

或者这是不可能的,我仍然需要记住分数列表吗?

or is this impossible and I still need to remember the list of scores?

推荐答案

以下公式可让您根据需要从存储的平均值和计数中跟踪平均值.

The following formulas allow you to track averages just from stored average and count, as you requested.

currentScore = (currentScore * currentCount + newValue) / (currentCount + 1) currentCount = currentCount + 1

这取决于您的平均值是当前的总和除以计数的事实.因此,您只需将计数乘以平均值即可得到总和,将新值相加并除以(count + 1),然后增加计数.

This relies on the fact that your average is currently your sum divided by the count. So you simply multiply count by average to get the sum, add your new value and divide by (count+1), then increase count.

因此,假设您有数据{7,9,11,1,12},并且唯一要保留的是平均值和计数.随着每个数字的添加,您将得到:

So, let's say you have the data {7,9,11,1,12} and the only thing you're keeping is the average and count. As each number is added, you get:

+--------+-------+----------------------+----------------------+ | Number | Count | Actual average | Calculated average | +--------+-------+----------------------+----------------------+ | 7 | 1 | (7)/1 = 7 | (0 * 0 + 7) / 1 = 7 | | 9 | 2 | (7+9)/2 = 8 | (7 * 1 + 9) / 2 = 8 | | 11 | 3 | (7+9+11)/3 = 9 | (8 * 2 + 11) / 3 = 9 | | 1 | 4 | (7+9+11+1)/4 = 7 | (9 * 3 + 1) / 4 = 7 | | 12 | 5 | (7+9+11+1+12)/5 = 8 | (7 * 4 + 12) / 5 = 8 | +--------+-------+----------------------+----------------------+

更多推荐

我可以计算这些数字的平均值吗?

本文发布于:2023-10-28 05:22:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535650.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!