Is there a very simple algorithm to figure out which of 4 numbers is the greatest?
是否有一个非常简单的算法来确定4个数字中哪一个最大?
11 个解决方案
#1
If you're using a language that supports some sort of max function or array sorting definitely use those features. Or choose any of the other sane answers in this thread. However, just for fun:
如果您使用的语言支持某种最大函数或数组排序,请务必使用这些功能。或者在这个帖子中选择任何其他合理的答案。但是,只是为了好玩:
maximum = (var1 > var2 ? var1 : var2) > (var3 > var 4 ? var3 : var 4) ?
(var1 > var2 ? var1 : var2) :
(var3 > var 4 ? var3 : var 4);
#2
var lst = new List<int>() { 1, 7, 3, 4 };
var max = lst.Max();
I got no VB, but you get the idea.
我没有VB,但你明白了。
#3
If they are in an array, something like this should work:
如果它们在一个数组中,这样的东西应该工作:
VB:
Dim ar As Integer() = {3, 6, 9, 12}
Dim largest As Integer = ar(0)
For i As Integer = 1 To ar.Length - 1
If ar(i) > largest Then
largest = ar(i)
End If
Next
C#:
int[] ar = {3, 6, 9, 12};
int largest = ar[0];
for(int i = 1; i < ar.Length; i++) {
if(ar[i] > largest) {
largest = ar[i];
}
}
#4
Put the numbers into an array, sort the array, then select the one whose index is array length -1.
将数字放入数组,对数组进行排序,然后选择索引为数组长度为-1的数组。
Or you could put the numbers into an array, sort the array, reverse the array, and then select index 0.
或者您可以将数字放入数组,对数组进行排序,反转数组,然后选择索引0。
If you need to write your own sorting algorithm, the simplest one to implement is likely to be the bubble sort.
如果您需要编写自己的排序算法,最简单的实现可能是冒泡排序。
#5
With VB.Net you could the following and it will work for any number of numbers
使用VB.Net,您可以使用以下内容,它可以用于任意数量的数字
Public Function Max(ParamArray items As Integer()) As Integer
if items.Length = 0 Then
throw New ArgumentException("need at least 1 number")
End IF
return items.Max()
End Function
Then you can now do
那你现在可以做
Max(1,2,3,4)
#6
There are plenty of ways you could do this. A really naive approach would be:
有很多方法可以做到这一点。一个非常天真的方法是:
#Pseudocode
If number1 > number2 and number1 > number3 and number1 > number4: return number1
Else if number2 > number3 and number2 > number4: return number2
Else if number3 > number4: return number3
Else: return number4
It's more practical to use arrays but if you're starting that could be more complicated than simple if blocks.
使用数组更实际,但是如果你开始使用数组比使用数组简单更复杂。
#7
If they're in an array - and doing it explicitly rather than using sort:
如果它们在数组中 - 并且明确地执行它而不是使用sort:
int max = int.MinValue; // i.e. the "largest" negative number
int largest = -1;
for (int index = 0; index < array.Length; index++)
{
if (array[index] > max)
{
max = array[index];
largest = index;
}
}
The greatest value will be max
and it's index in largest
.
最大值将是max,它的索引最大。
Nate's answer is more efficient as it uses the first element of the array as the initial value. So the first three lines of my solution would become:
Nate的答案更有效,因为它使用数组的第一个元素作为初始值。所以我的解决方案的前三行将成为:
int max = array[0];
int largest = 0;
for (int index = 0; index < array.Length; index++)
#8
- Get A, B, C, D from user;
- largest = A;
- if B > largest then
- Largest = B;
- if C > largest then
- Largest = C;
- if D > largest then
- largest = D;
- Print largest
从用户那里获得A,B,C,D;
最大= A;
如果B>最大那么
最大= B;
如果C>最大那么
最大= C;
如果D>最大那么
最大= D;
#9
In Java, if a is an int[4]:
在Java中,如果a是int [4]:
Math.max(Math.max(a[0], a[1]), Math.max(a[2], a[3]))
Math.max(Math.max(a [0],a [1]),Math.max(a [2],a [3]))
#10
this is my own analization. i made this code to display the lowest and highest numbers among the 4 inputted numbers from the textbox,. it will display the lowest and highest to appointed labels. if u input two same lowest or highest numbers, a msgbox appear to notify u somehow that u inputted same highest or lowest numbers and it displays back to its appropriate label. i used labels for the display of lowest and highest. here's my fb: iver saladaga anoos , 2ndyear student of JHCSC tambulig, zamboanga del sur, philippines.. so here it is! it worked for me. im using vb6 enterprise edition. :)
这是我自己的分析。我使用此代码显示文本框中4个输入数字中的最低和最高数字。它将显示指定标签的最低和最高。如果您输入两个相同的最低或最高数字,msgbox似乎以某种方式通知您输入相同的最高或最低数字,并显示回其相应的标签。我使用标签显示最低和最高。这是我的fb:iver saladaga anoos,JHCSC tambulig,zamboanga del sur,菲律宾的第二年学生..所以在这里!它对我有用。即时通讯使用vb6企业版。 :)
Private Sub Command1_Click()
Dim A, B, C, D As Long
A = Text1.Text
B = Text2.Text
C = Text3.Text
D = Text4.Text
If A < B And A < C And A < D Then
Label9.Caption = A
Else
If A > B And A > C And A > D Then
Label10.Caption = A
End If
End If
If A < B And A < D And A < C Then Label9.Caption = A Else If A > B And A > D And A > C Then Label10.Caption = A End If End If
如果A B和A> D和A> C然后Label10.Caption = A End If End If 和a>
If A < C And A < B And A < D Then Label9.Caption = A Else If A > C And A > B And A > D Then Label10.Caption = A End If End If
如果A
If A < C And A < D And A < B Then Label9.Caption = A Else If A > C And A > D And A > B Then Label10.Caption = A End If End If
如果A
If A < D And A < C And A < B Then Label9.Caption = A Else If A > D And A > C And A > B Then Label10.Caption = A End If End If
如果A
If A < D And A < B And A < C Then Label9.Caption = A Else If A > D And A > B And A > C Then Label10.Caption = A End If End If
如果A
If B < C And B < A And B < D Then Label9.Caption = B Else If B > C And B > A And B > D Then Label10.Caption = B End If End If
如果B
If B < C And B < D And B < A Then Label9.Caption = B Else If B > C And B > D And B > A Then Label10.Caption = B End If End If
如果B
If B < A And B < C And B < D Then Label9.Caption = B Else If B > A And B > C And B > D Then Label10.Caption = B End If End If
如果B A和B> C和B> D那么Label10.Caption = B End If End If 和b>
If B < A And B < D And B < C Then Label9.Caption = B Else If B > A And B > D And B > C Then Label10.Caption = B End If End If
如果B A和B> D和B> C然后Label10.Caption = B结束如果结束If 和b>
If B < D And B < C And B < A Then Label9.Caption = B Else If B > D And B > C And B > A Then Label10.Caption = B End If End If
如果B
If B < D And B < A And B < C Then Label9.Caption = B Else If B > D And B > A And B > C Then Label10.Caption = B End If End If
如果B
If C < A And C < B And C < D Then Label9.Caption = C Else If C > A And C > B And C > D Then Label10.Caption = C End If End If
如果C A和C> B和C> D然后Label10.Caption = C End If End If 和c>
If C < A And C < D And C < B Then Label9.Caption = C Else If C > A And C > D And C > B Then Label10.Caption = C End If End If
如果C A和C> D和C> B然后Label10.Caption = C End If End If 和c>
If C < B And C < A And C < D Then Label9.Caption = C Else If C > B And C > A And C > D Then Label10.Caption = C End If End If
如果C B和C> A和C> D那么Label10.Caption = C End If End If 和c>
If C < B And C < D And C < A Then
Label9.Caption = C
Else If C > B And C > D And C > A Then Label10.Caption = C End If End If
否则如果C> B和C> D和C> A然后Label10.Caption = C End If End If
If C < D And C < A And C < B Then
Label9.Caption = C
Else
If C > D And C > A And C > B Then
Label10.Caption = C
End If
End If
If C < D And C < B And C < A Then
Label9.Caption = C
Else
If C > D And C > B And C > A Then
Label10.Caption = C
End If
End If
If D < A And D < B And D < C Then
Label9.Caption = D
Else If D > A And D > B And D > C Then Label10.Caption = D End If End If
否则如果D> A和D> B并且D> C那么Label10.Caption = D End If End If
If D < A And D < C And D < B Then
Label9.Caption = D
Else If D > A And D > C And D > B Then Label10.Caption = D End If End If
否则如果D> A和D> C并且D> B那么Label10.Caption = D End If End If
If D < B And D < A And D < C Then
Label9.Caption = D
Else If D > B And D > A And D > C Then Label10.Caption = D End If End If
否则如果D> B和D> A和D> C则Label10.Caption = D End If End If
If D < B And D < C And D < A Then
Label9.Caption = D
Else
If D > B And D > C And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < B And D < A Then
Label9.Caption = D
Else
If D > C And D > B And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < A And D < B Then
Label9.Caption = D
Else
If D > C And D > A And D > B Then
Label10.Caption = D
End If
End If
Command2.Enabled = True
If A = D And A > C And A > B Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = D And A < C And A < B Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If A = B And A > C And A > D Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = B And A < C And A < D Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If B = D And B > A And B > C Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = D And B < A And B < C Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If B = C And B > D And B > A Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = C And B < D And B < A Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If C = A And C > D And C > B Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = A And C < D And C < B Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
If C = D And C > B And C > A Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = D And C < B And C < A Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
End Sub
Private Sub Command2_Click()
Text1.Text = Clear
Text2.Text = Clear
Text3.Text = Clear
Text4.Text = Clear
Label9.Caption = Clear
Label10.Caption = Clear
Command2.Enabled = False
Command1.Enabled = False
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Command2.Enabled = False
Command1.Enabled = False
End Sub
Private Sub Text1_Change()
Command1.Enabled = True
End Sub
#11
My first question would be why? Second would be, if it's only four numbers then it really doesn't matter. Whatever takes your fancy. I personally would go with the fewest lines of code. Which would be to use the built in array.Sort method, then take the last item.
I would also consider using LINQ, just because you can.
Or Math.Max in a nasty nested way so Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))
If there could be hundreds of numbers, then I would try and pick a better algorithm. Probably the one suggested by @ChrisF, although it would depend on where the numbers are coming from, EG a database could find the max much easier, or if the numbers are being read from somewhere sequentially then you could store the max as you read the numbers.
我的第一个问题是为什么?第二个是,如果它只有四个数字那么它真的没关系。无论你喜欢什么。我个人会使用最少的代码行。哪个是使用内置的array.Sort方法,然后取最后一项。我也会考虑使用LINQ,因为你可以。或者Math.Max以一种令人讨厌的嵌套方式,所以Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))如果可能有数百个数字,那么我会尝试选择一个更好的算法。可能是@ChrisF建议的那个,虽然它取决于数字的来源,EG数据库可以更容易地找到最大值,或者如果从某个地方顺序读取数字,那么你可以在读取时存储最大值数字。
#1
If you're using a language that supports some sort of max function or array sorting definitely use those features. Or choose any of the other sane answers in this thread. However, just for fun:
如果您使用的语言支持某种最大函数或数组排序,请务必使用这些功能。或者在这个帖子中选择任何其他合理的答案。但是,只是为了好玩:
maximum = (var1 > var2 ? var1 : var2) > (var3 > var 4 ? var3 : var 4) ?
(var1 > var2 ? var1 : var2) :
(var3 > var 4 ? var3 : var 4);
#2
var lst = new List<int>() { 1, 7, 3, 4 };
var max = lst.Max();
I got no VB, but you get the idea.
我没有VB,但你明白了。
#3
If they are in an array, something like this should work:
如果它们在一个数组中,这样的东西应该工作:
VB:
Dim ar As Integer() = {3, 6, 9, 12}
Dim largest As Integer = ar(0)
For i As Integer = 1 To ar.Length - 1
If ar(i) > largest Then
largest = ar(i)
End If
Next
C#:
int[] ar = {3, 6, 9, 12};
int largest = ar[0];
for(int i = 1; i < ar.Length; i++) {
if(ar[i] > largest) {
largest = ar[i];
}
}
#4
Put the numbers into an array, sort the array, then select the one whose index is array length -1.
将数字放入数组,对数组进行排序,然后选择索引为数组长度为-1的数组。
Or you could put the numbers into an array, sort the array, reverse the array, and then select index 0.
或者您可以将数字放入数组,对数组进行排序,反转数组,然后选择索引0。
If you need to write your own sorting algorithm, the simplest one to implement is likely to be the bubble sort.
如果您需要编写自己的排序算法,最简单的实现可能是冒泡排序。
#5
With VB.Net you could the following and it will work for any number of numbers
使用VB.Net,您可以使用以下内容,它可以用于任意数量的数字
Public Function Max(ParamArray items As Integer()) As Integer
if items.Length = 0 Then
throw New ArgumentException("need at least 1 number")
End IF
return items.Max()
End Function
Then you can now do
那你现在可以做
Max(1,2,3,4)
#6
There are plenty of ways you could do this. A really naive approach would be:
有很多方法可以做到这一点。一个非常天真的方法是:
#Pseudocode
If number1 > number2 and number1 > number3 and number1 > number4: return number1
Else if number2 > number3 and number2 > number4: return number2
Else if number3 > number4: return number3
Else: return number4
It's more practical to use arrays but if you're starting that could be more complicated than simple if blocks.
使用数组更实际,但是如果你开始使用数组比使用数组简单更复杂。
#7
If they're in an array - and doing it explicitly rather than using sort:
如果它们在数组中 - 并且明确地执行它而不是使用sort:
int max = int.MinValue; // i.e. the "largest" negative number
int largest = -1;
for (int index = 0; index < array.Length; index++)
{
if (array[index] > max)
{
max = array[index];
largest = index;
}
}
The greatest value will be max
and it's index in largest
.
最大值将是max,它的索引最大。
Nate's answer is more efficient as it uses the first element of the array as the initial value. So the first three lines of my solution would become:
Nate的答案更有效,因为它使用数组的第一个元素作为初始值。所以我的解决方案的前三行将成为:
int max = array[0];
int largest = 0;
for (int index = 0; index < array.Length; index++)
#8
- Get A, B, C, D from user;
- largest = A;
- if B > largest then
- Largest = B;
- if C > largest then
- Largest = C;
- if D > largest then
- largest = D;
- Print largest
从用户那里获得A,B,C,D;
最大= A;
如果B>最大那么
最大= B;
如果C>最大那么
最大= C;
如果D>最大那么
最大= D;
#9
In Java, if a is an int[4]:
在Java中,如果a是int [4]:
Math.max(Math.max(a[0], a[1]), Math.max(a[2], a[3]))
Math.max(Math.max(a [0],a [1]),Math.max(a [2],a [3]))
#10
this is my own analization. i made this code to display the lowest and highest numbers among the 4 inputted numbers from the textbox,. it will display the lowest and highest to appointed labels. if u input two same lowest or highest numbers, a msgbox appear to notify u somehow that u inputted same highest or lowest numbers and it displays back to its appropriate label. i used labels for the display of lowest and highest. here's my fb: iver saladaga anoos , 2ndyear student of JHCSC tambulig, zamboanga del sur, philippines.. so here it is! it worked for me. im using vb6 enterprise edition. :)
这是我自己的分析。我使用此代码显示文本框中4个输入数字中的最低和最高数字。它将显示指定标签的最低和最高。如果您输入两个相同的最低或最高数字,msgbox似乎以某种方式通知您输入相同的最高或最低数字,并显示回其相应的标签。我使用标签显示最低和最高。这是我的fb:iver saladaga anoos,JHCSC tambulig,zamboanga del sur,菲律宾的第二年学生..所以在这里!它对我有用。即时通讯使用vb6企业版。 :)
Private Sub Command1_Click()
Dim A, B, C, D As Long
A = Text1.Text
B = Text2.Text
C = Text3.Text
D = Text4.Text
If A < B And A < C And A < D Then
Label9.Caption = A
Else
If A > B And A > C And A > D Then
Label10.Caption = A
End If
End If
If A < B And A < D And A < C Then Label9.Caption = A Else If A > B And A > D And A > C Then Label10.Caption = A End If End If
如果A B和A> D和A> C然后Label10.Caption = A End If End If 和a>
If A < C And A < B And A < D Then Label9.Caption = A Else If A > C And A > B And A > D Then Label10.Caption = A End If End If
如果A
If A < C And A < D And A < B Then Label9.Caption = A Else If A > C And A > D And A > B Then Label10.Caption = A End If End If
如果A
If A < D And A < C And A < B Then Label9.Caption = A Else If A > D And A > C And A > B Then Label10.Caption = A End If End If
如果A
If A < D And A < B And A < C Then Label9.Caption = A Else If A > D And A > B And A > C Then Label10.Caption = A End If End If
如果A
If B < C And B < A And B < D Then Label9.Caption = B Else If B > C And B > A And B > D Then Label10.Caption = B End If End If
如果B
If B < C And B < D And B < A Then Label9.Caption = B Else If B > C And B > D And B > A Then Label10.Caption = B End If End If
如果B
If B < A And B < C And B < D Then Label9.Caption = B Else If B > A And B > C And B > D Then Label10.Caption = B End If End If
如果B A和B> C和B> D那么Label10.Caption = B End If End If 和b>
If B < A And B < D And B < C Then Label9.Caption = B Else If B > A And B > D And B > C Then Label10.Caption = B End If End If
如果B A和B> D和B> C然后Label10.Caption = B结束如果结束If 和b>
If B < D And B < C And B < A Then Label9.Caption = B Else If B > D And B > C And B > A Then Label10.Caption = B End If End If
如果B
If B < D And B < A And B < C Then Label9.Caption = B Else If B > D And B > A And B > C Then Label10.Caption = B End If End If
如果B
If C < A And C < B And C < D Then Label9.Caption = C Else If C > A And C > B And C > D Then Label10.Caption = C End If End If
如果C A和C> B和C> D然后Label10.Caption = C End If End If 和c>
If C < A And C < D And C < B Then Label9.Caption = C Else If C > A And C > D And C > B Then Label10.Caption = C End If End If
如果C A和C> D和C> B然后Label10.Caption = C End If End If 和c>
If C < B And C < A And C < D Then Label9.Caption = C Else If C > B And C > A And C > D Then Label10.Caption = C End If End If
如果C B和C> A和C> D那么Label10.Caption = C End If End If 和c>
If C < B And C < D And C < A Then
Label9.Caption = C
Else If C > B And C > D And C > A Then Label10.Caption = C End If End If
否则如果C> B和C> D和C> A然后Label10.Caption = C End If End If
If C < D And C < A And C < B Then
Label9.Caption = C
Else
If C > D And C > A And C > B Then
Label10.Caption = C
End If
End If
If C < D And C < B And C < A Then
Label9.Caption = C
Else
If C > D And C > B And C > A Then
Label10.Caption = C
End If
End If
If D < A And D < B And D < C Then
Label9.Caption = D
Else If D > A And D > B And D > C Then Label10.Caption = D End If End If
否则如果D> A和D> B并且D> C那么Label10.Caption = D End If End If
If D < A And D < C And D < B Then
Label9.Caption = D
Else If D > A And D > C And D > B Then Label10.Caption = D End If End If
否则如果D> A和D> C并且D> B那么Label10.Caption = D End If End If
If D < B And D < A And D < C Then
Label9.Caption = D
Else If D > B And D > A And D > C Then Label10.Caption = D End If End If
否则如果D> B和D> A和D> C则Label10.Caption = D End If End If
If D < B And D < C And D < A Then
Label9.Caption = D
Else
If D > B And D > C And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < B And D < A Then
Label9.Caption = D
Else
If D > C And D > B And D > A Then
Label10.Caption = D
End If
End If
If D < C And D < A And D < B Then
Label9.Caption = D
Else
If D > C And D > A And D > B Then
Label10.Caption = D
End If
End If
Command2.Enabled = True
If A = D And A > C And A > B Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = D And A < C And A < B Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If A = B And A > C And A > D Then
MsgBox "Same highest numbers (" + A + ") is inputted."
highest = A
Label10.Caption = highest
Else
If A = B And A < C And A < D Then
MsgBox "Same lowest numbers (" + A + ") is inputted."
lowest = A
Label9.Caption = lowest
End If
End If
If B = D And B > A And B > C Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = D And B < A And B < C Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If B = C And B > D And B > A Then
MsgBox "Same highest numbers (" + B + ") is inputted."
highest = B
Label10.Caption = highest
Else
If B = C And B < D And B < A Then
MsgBox "Same lowest numbers (" + B + ") is inputted."
lowest = B
Label9.Caption = lowest
End If
End If
If C = A And C > D And C > B Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = A And C < D And C < B Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
If C = D And C > B And C > A Then
MsgBox "Same highest numbers (" + C + ") is inputted."
highest = C
Label10.Caption = highest
Else
If C = D And C < B And C < A Then
MsgBox "Same lowest numbers (" + C + ") is inputted."
lowest = C
Label9.Caption = lowest
End If
End If
End Sub
Private Sub Command2_Click()
Text1.Text = Clear
Text2.Text = Clear
Text3.Text = Clear
Text4.Text = Clear
Label9.Caption = Clear
Label10.Caption = Clear
Command2.Enabled = False
Command1.Enabled = False
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
Private Sub Form_Load()
Command2.Enabled = False
Command1.Enabled = False
End Sub
Private Sub Text1_Change()
Command1.Enabled = True
End Sub
#11
My first question would be why? Second would be, if it's only four numbers then it really doesn't matter. Whatever takes your fancy. I personally would go with the fewest lines of code. Which would be to use the built in array.Sort method, then take the last item.
I would also consider using LINQ, just because you can.
Or Math.Max in a nasty nested way so Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))
If there could be hundreds of numbers, then I would try and pick a better algorithm. Probably the one suggested by @ChrisF, although it would depend on where the numbers are coming from, EG a database could find the max much easier, or if the numbers are being read from somewhere sequentially then you could store the max as you read the numbers.
我的第一个问题是为什么?第二个是,如果它只有四个数字那么它真的没关系。无论你喜欢什么。我个人会使用最少的代码行。哪个是使用内置的array.Sort方法,然后取最后一项。我也会考虑使用LINQ,因为你可以。或者Math.Max以一种令人讨厌的嵌套方式,所以Math.Max(Number1,Math.Max(Number2,Math.Max(Number3,Number4))))如果可能有数百个数字,那么我会尝试选择一个更好的算法。可能是@ChrisF建议的那个,虽然它取决于数字的来源,EG数据库可以更容易地找到最大值,或者如果从某个地方顺序读取数字,那么你可以在读取时存储最大值数字。