Advertisement
imjyb1008

第六題

Dec 6th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.08 KB | None | 0 0
  1. Private Sub cmdStart_Click()
  2.  
  3.     Cls
  4.  
  5.     Dim n As Integer
  6.     Do
  7.         temp = InputBox("請輸入數字列中的數字個數")
  8.         If temp = "" Then Exit Sub
  9.         n = temp
  10.     Loop While Not (n > 0 And n <= 1000)
  11.    
  12.     ReDim source(n) As Integer
  13.  
  14.     Dim i As Integer
  15.     For i = 1 To n
  16.        
  17.         Do
  18.             temp = InputBox("請輸入大於等於0的整數", "第" & i & "個")
  19.             If temp = "" Then Exit Sub
  20.             source(i) = temp
  21.         Loop While source(i) < 0
  22.    
  23.     Next i
  24.    
  25.     Dim min As Integer
  26.     min = 32767
  27.     Dim sum As Long
  28.     sum = 0
  29.    
  30.     Dim a As String
  31.     For i = 0 To 32766
  32.    
  33.         Dim j As Integer
  34.         For j = 1 To n
  35.             sum = sum + Abs(source(j) - i)
  36.         Next j
  37.        
  38.         If sum <= min Then
  39.            
  40.             If sum = min Then
  41.                 a = a & "," & i
  42.             Else
  43.                 a = i
  44.             End If
  45.            
  46.             min = sum
  47.        
  48.         End If
  49.    
  50.         sum = 0
  51.    
  52.     Next i
  53.    
  54.     Print "A=" & a
  55.    
  56. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement