比较变量的简单逻辑问题
如果我有带有数字的变量,如何找出值最高的三个变量?
If I have variables with numbers how to figure out which are the three with the highest value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如果我有带有数字的变量,如何找出值最高的三个变量?
If I have variables with numbers how to figure out which are the three with the highest value?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
您可以手动迭代它们并提取前 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
我不想说我是新手,因为我不认为我是。然而这个问题却很难回答。我编写 PHP 代码才大约 8 个月,我想有更好的方法来实现这样的效果。根据我的技能,我选择使用 PHP 并得出以下结论:
细分
你好运!
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:
The breakdown
Good luck!
这实际上取决于您使用的语言。
我建议将它们全部放入一个数组中,从最高到最低排序,然后前三个元素将是最高的。
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.
您可以按降序对它们进行排序,前三个将是您想要的。
You may sort them in descending order and the first three will be the ones you want.
不要忘记这里有一些奇怪的情况:
最高值出现超过 3 次的情况,因此不只有一个正确答案。例如,如果有十几个变量的值都为零。
如果此类程序的变量少于 3 个,则可能会出现问题。
Don't forget that there are a couple odd cases here:
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.
If you have less than 3 variables for such a program there could be a problem.