Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub cmdStart_Click()
- Cls
- Dim n As Integer
- Do
- temp = InputBox("請輸入數字列中的數字個數")
- If temp = "" Then Exit Sub
- n = temp
- Loop While Not (n > 0 And n <= 1000)
- ReDim source(n) As Integer
- Dim i As Integer
- For i = 1 To n
- Do
- temp = InputBox("請輸入大於等於0的整數", "第" & i & "個")
- If temp = "" Then Exit Sub
- source(i) = temp
- Loop While source(i) < 0
- Next i
- Dim min As Integer
- min = 32767
- Dim sum As Long
- sum = 0
- Dim a As String
- For i = 0 To 32766
- Dim j As Integer
- For j = 1 To n
- sum = sum + Abs(source(j) - i)
- Next j
- If sum <= min Then
- If sum = min Then
- a = a & "," & i
- Else
- a = i
- End If
- min = sum
- End If
- sum = 0
- Next i
- Print "A=" & a
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement