比较变量的简单逻辑问题

发布于 2024-08-16 03:36:15 字数 32 浏览 1 评论 0原文

如果我有带有数字的变量,如何找出值最高的三个变量?

If I have variables with numbers how to figure out which are the three with the highest value?

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

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

发布评论

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

评论(5

遇见了你 2024-08-23 03:36:15

您可以手动迭代它们并提取前 3 个(以某种形式维护到目前为止您拥有的信息),或者只是对它们进行排序并取出前 3 个

You can either manually iterate through them and extract the top 3 (maintaining the information you have so far in some form) or just sort them all and take the top 3

潇烟暮雨 2024-08-23 03:36:15

我不想说我是新手,因为我不认为我是。然而这个问题却很难回答。我编写 PHP 代码才大约 8 个月,我想有更好的方法来实现这样的效果。根据我的技能,我选择使用 PHP 并得出以下结论:

<?php

$variable = array();
$variable[1] = 15;
$variable[2] = 30;
$variable[3] = 9;
$variable[4] = 86;
$variable[5] = 46;
$variable[6] = 12;
$variable[7] = 86;

## Clean the array of duplicates
$variable = array_unique($variable);

## Sort array from greatest to lease in DESC order
rsort($variable);


for ($i = 0; $i < 3; $i++):
    echo $variable[$i]."<br />";
endfor;

?>

细分

  • 首先声明您的数组
  • 然后您的数组项
  • 使用“array_unique”清理数组以删除重复的值
  • 使用“rsort”对数组从最大到最小进行排序
  • 启动for 循环,其中 $i 等于 0,仅当 $i 小于 3 时运行,每次迭代为 $i 加 1
  • 回显数组项 祝

你好运!

I don't want to say that I'm a newb, because I don't think I am. However this question was rather difficult to answer. I've only been coding PHP for around 8 months, and I imagine that there is a much better way of accomplishing such an effect. Based on my skill set I choose to use PHP and came up with this:

<?php

$variable = array();
$variable[1] = 15;
$variable[2] = 30;
$variable[3] = 9;
$variable[4] = 86;
$variable[5] = 46;
$variable[6] = 12;
$variable[7] = 86;

## Clean the array of duplicates
$variable = array_unique($variable);

## Sort array from greatest to lease in DESC order
rsort($variable);


for ($i = 0; $i < 3; $i++):
    echo $variable[$i]."<br />";
endfor;

?>

The breakdown

  • First declare your array
  • Then your array items
  • Clean the array with "array_unique" to remove reiterated values
  • Use "rsort" to sort array from greatest to least
  • Initiate a for loop in which $i equals 0, run only while $i is less then 3, add one to $i for each iteration
  • Echo the array item

Good luck!

羅雙樹 2024-08-23 03:36:15

这实际上取决于您使用的语言。

我建议将它们全部放入一个数组中,从最高到最低排序,然后前三个元素将是最高的。

This really depends on the language you're using.

I would suggest putting them all in an array, sort them from highest to lowest, and then the first three elements would be your highest.

╰つ倒转 2024-08-23 03:36:15

您可以按降序对它们进行排序,前三个将是您想要的。

You may sort them in descending order and the first three will be the ones you want.

彼岸花似海 2024-08-23 03:36:15

不要忘记这里有一些奇怪的情况:

  1. 最高值出现超过 3 次的情况,因此不只有一个正确答案。例如,如果有十几个变量的值都为零。

  2. 如果此类程序的变量少于 3 个,则可能会出现问题。

Don't forget that there are a couple odd cases here:

  1. A case where the highest value occurs more than 3 times so that there isn't just one correct answer. For example, if there a dozen variables all with a value of zero.

  2. If you have less than 3 variables for such a program there could be a problem.

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