如何找到在一个数组中最小的和最大的号码是多少? [英] How to find the smallest and biggest number in an array?

查看:141
本文介绍了如何找到在一个数组中最小的和最大的号码是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我怎么能找到在Delphi中最小的和最大的号码是多少?

Hello how can I find the smallest and biggest number in delphi?

假如我有存储在数组中10个不同的数字:

Suppose I have 10 different numbers stored in an array:

我如何才能找到我的阵列的最大数,最小数?

How can I find the biggest number and smallest numbers in my array?

推荐答案

只需通过线性阵列循环。保持变量的最小值,一个用于最大值。初始化同时向阵列中的第一个值。然后,对于每个元件,更新最小或最大值,如果该元素比min或分别最大值小于或大于

Simply loop through the array in linear fashion. Keep a variable for the min value and one for the max values. Initialise both to the first value in the array. Then for each element, update the min or max value if that element is less than or greater than the min or max value respectively.

minval := a[0];
maxval := a[0];
for i := 1 to Count-1 do
begin
  if a[i]<minval then
    minval := a[i]
  else if a[i]>maxval then
    maxval := a[i];
end;

显然,这code假定计数> 0

Obviously this code assumes Count>0.

请注意,您同样可以使用MINVALUE和MaxValue的程序从数学单元。

Note that you could equally use the MinValue and MaxValue routines from the Math unit.

这篇关于如何找到在一个数组中最小的和最大的号码是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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