Fortran中如何计算级数的众数

发布于 2024-11-03 13:22:23 字数 93 浏览 0 评论 0原文

如何使用 Fortran 计算级数的众数?

例如:

1,2,2,3,3,3,4,4,5
Mode = 3

How can I calculate the mode of a series using Fortran?

For example:

1,2,2,3,3,3,4,4,5
Mode = 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶情人眼里出诗心の 2024-11-10 13:22:23

如果您的数字已排序(正如它们看起来的那样),则伪代码很简单:

set maxval to -1
set maxcount to -1
set count to -1
set lastval to list[0] - 1
for every val in list:
    if val is not equal to lastval:
        if count is greater than maxcount:
            set maxval to lastval
            set maxcount to count
        set count to 0
        set lastval to val
    set count to count plus one
if maxcount is not equal to -1:
    print "mode is " maxval " with count of " maxcount

请记住,如果有多个,这将仅返回第一个模式。

If your numbers are sorted (as they appear to be), the pseudo-code is simple:

set maxval to -1
set maxcount to -1
set count to -1
set lastval to list[0] - 1
for every val in list:
    if val is not equal to lastval:
        if count is greater than maxcount:
            set maxval to lastval
            set maxcount to count
        set count to 0
        set lastval to val
    set count to count plus one
if maxcount is not equal to -1:
    print "mode is " maxval " with count of " maxcount

Keep in mind that this will return only the first mode if there is more than one.

記柔刀 2024-11-10 13:22:23

如果您需要的话,您可以在那里找到已经编写的代码,并且它不仅仅是一个练习;例如
维基 Rosettacode.org 上的模式。如果是练习,请首先尝试遵循其他答案中给出的算法。

You can find already made code out there, if you need it and it is not just an exercize; e.g.
Mode at wiki rosettacode.org. If it is an exercize, try first to follow the algorithm given in the other answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文