如何计算C#中的模式,中位数和范围? [英] How to work out the mode, median and range in C#?

查看:399
本文介绍了如何计算C#中的模式,中位数和范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从学生开始的,我需要计算3组数字的模式,中位数和范围。如果有人可以帮我学习如何在C#中找到这些平均值,我会非常感激!

I'm starting out as a student and I need to work out the mode, median and range for 3 sets of numbers. If someone could help me learn how to find these averages in C# I would really appreciate it!

这些数字是: £1.50£1.80£3.65£3.80£4.20£1.00£3.65 £1.10£1.35£2.80£1.30£1.10£5.60£1.10£3.70£2.25£4.00£1.60£2.00

The numbers are: £1.50 £1.80 £3.65 £3.80 £4.20 £1.00 £3.65 £1.10 £1.35 £2.80 £1.30 £1.10 £5.60 £1.10 £3.70 £2.25 £4.00 £1.60 £2.00

推荐答案

模式是列表中更频繁重复的数字。如果你想以"聪明"的方式做到这一点。方式(而不是编写将它们排列到集合中的循环),您可以使用按数字值分组的linq查询,按计数
降序排序,然后获取第一个条目。

The mode is the number that repeats more often in the list. If you want to do it in a "clever" way (instead of writing a loop that arranges them into a collection), you can use a linq query that groups by the value of the number, sorts by count descending and then takes the first entry.

中位数是排序时位于列表中间的数字。你可以做到这一点:对列表进行排序(它也可以使用Linq完成,或者你可以使用Array.Sort)然后如果有奇数个条目则在中间取数字。
如果有一个偶数(所以没有数字正好在中间),你可以返回中间两边的两个数字的平均值。

The median is the number that sits in the middle of the list when it is sorted. You can do precisely that: sort the list (it can also be done with Linq, or you can use Array.Sort) and then take the number in the middle if there is an odd number of entries. If there is an even number (so none of the numbers is exactly in the middle), you return the average of the two numbers that are on both sides of the middle.

我不完全确定你的意思是"范围"。如果你指的是最大和最小数字之间的差异,那么一旦你按照上一步的要求对所有数字进行排序就很容易:只需从最后一个数字中减去
的第一个数字。

I'm not entirely sure about what you mean by "range". If you are referring to the difference between the largest and smallest number, then it is very easy once you have all the numbers sorted as required by the previous step: just subtract the first number from the last one.

是的,这是一个非常"通用"的产品。回答并没有提供代码示例。当学生要求练习帮助时,这是论坛的标准做法。我们可以提供通用指南,但不能写完已经完成的解决方案,以便
复制和粘贴 - 这将是作弊。

Yes, this is a very "generic" answer and doesn't provide a code example. This is the standard practice in the forums when a student asks for help with an exercise. We can provide generic guidance, but not write the completed solution ready for copying and pasting -- that would be cheating.


这篇关于如何计算C#中的模式,中位数和范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆