Public Class Form1
Dim i
Dim str1 = "未找到"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s = Val(InputBox("請輸入要找的值?"))
'Dim s = 22
Dim a() = {2, 1, 5, 10, 22, 34, 17}
Array.Sort(a)
Dim low = 0
Dim high = UBound(a)
Dim m = 99999
While low <= high And s <> m
m = Int((low + high) / 2)
If a(m) = s Then
str1 = "找到了在排序後的註標" & m & "處"
Exit While
Else
If a(m) < s Then
low = m + 1
Else
high = m - 1
End If
End If
End While
MsgBox(str1)
End Sub
End Class
2008年12月31日 星期三
二元搜尋 -- 料一甲55王小明
循序搜尋 -- 精簡版
Public Class Form1
Dim i
Dim str1 = "未找到"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s = Val(InputBox("請輸入要找的值?"))
Dim a() = {2, 1, 5, 10, 22, 34, 17}
For i = 0 To 6
If a(i) = s Then
str1 = "找到了在註標" & i & "處"
End If
Next
MsgBox(str1)
End Sub
End Class
除錯練習 -- 二元搜尋
'請找出為何以下搜尋程式,輸入1及34找不到,而其他值都沒有問題
Public Class Form1
Dim i
Dim isFound = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s = Val(InputBox("請輸入要找的值?"))
'Dim s = 22
Dim sN
Dim a() = {2, 1, 5, 10, 22, 34, 17}
Array.Sort(a)
Dim low = 0
Dim high = UBound(a)
Dim m
While low < high And Not isFound
m = Int((low + high) / 2)
If a(m) = s Then
isFound = True
sN = m
Exit While
Else
If a(m) < s Then
low = m + 1
Else
high = m - 1
End If
End If
End While
If isFound = True Then
MsgBox("找到了在排序後的註標" & sN & "處")
MsgBox("也就是在排序後一般習慣稱的第" & sN + 1 & "個")
Else
MsgBox("未找到")
End If
End Sub
End Class
循序搜尋例
Public Class Form1
Dim i
Dim isFound = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s = Val(InputBox("請輸入要找的值?"))
Dim sN
Dim a() = {2, 1, 5, 10, 22, 34, 17}
For i = 0 To 6
If a(i) = s Then
isFound = True
sN = i
End If
Next
If isFound = True Then
MsgBox("找到了在註標" & sN & "處")
MsgBox("也就是一般習慣稱的第" & sN + 1 & "個")
Else
MsgBox("未找到")
End If
End Sub
End Class
2008年12月26日 星期五
20106黎怡君 99-1
'1.將輸入之秒數轉換成「時:分:秒」。如輸入 10000,則輸出 02:46:40
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t = 10000
Dim sec = t Mod 60
Dim minute = t \ 60 Mod 60
Dim hour = t \ 3600
MsgBox(hour & ":" & minute & ":" & sec)
End Sub
End Class
20107張純禎 99-1
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t = 10000
Dim sec = t Mod 60
Dim minute = t \ 60 Mod 60
Dim hour = t \ 3600
MsgBox(hour & ":" & minute & ":" & sec)
End Sub
End Class
20108陳一慈 99-1
'1.將輸入之秒數轉換成「時:分:秒」。如輸入 10000,則輸出 02:46:40。
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t = 10000
Dim sec = 10000 Mod 60
Dim minute = t \ 60 Mod 60
Dim hour = t \ 3600
MsgBox(hour & ":" & minute & ":" & sec)
End Sub
End Class
20122徐筱惠 99-1
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t = 10000
Dim sec = t Mod 60
Dim minute = t \ 60 Mod 60
Dim hour = t \ 3600
MsgBox(hour & ":" & minute & ":" & sec)
End Sub
End Class
麗山經典50題
http://203.64.52.1/~jennyher/research/problems50.txt
先從簡單的開始,慢慢就會進入狀況,有空就解個一二題,假以時日,您會寫出興趣來,也會很有成就感。
2008年12月24日 星期三
氣泡排序 -- 含過程
Public Class Form1
Dim i, j
Dim str1 = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'原資料
Dim a() = {5, 4, 3, 2, 1}
'排序前資料記錄
str1 = "原資料" & vbNewLine
str1 = str1 & a(0) & a(1) & a(2) & a(3) & a(4) & vbNewLine
str1 = str1 & "-- 過程 -- " & vbNewLine
'排序
For j = 0 To 3
For i = 0 To 3
If a(i) > a(i + 1) Then
Dim t = a(i)
a(i) = a(i + 1)
a(i + 1) = t
End If
Next i
'排序過程資料記錄
str1 = str1 & a(0) & a(1) & a(2) & a(3) & a(4) & vbNewLine
Next j
'最後結果輸出
MsgBox(str1)
End Sub
End Class
Visual Basic自學輔導注意事項
帳號:jackcyhs
虛擬教室7
教科書
教35 章節ch1-ch5 或 教34 前100題
測驗者姓名:班級座號姓名 例:料三甲50王小美
12/24 日測驗者以60分為及格分數
12/31日前自行上機補考者以70分為及格分數
排名排序Sample
Public Class Form1
Dim str1 = ""
Dim i, j
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'原始資料
Dim a() = {92, 83, 95, 71, 94}
Dim r(5)
'排序前資料輸出
For i = 0 To 4
str1 = str1 & a(i) & " "
Next
MsgBox(str1)
'排名排序
For j = 0 To 4
r(j) = 1
For i = 0 To 4
If a(j) < a(i) Then r(j) = r(j) + 1
Next
Next
'排序後資料輸出
str1 = ""
For i = 0 To 4
str1 = str1 & a(i) & " " & "名次:" & r(i) & vbNewLine
Next
MsgBox(str1)
End Sub
End Class
2008年12月23日 星期二
排名排序
Public Class Form1
Dim str1 = ""
Dim i, j
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'原始資料
Dim a() = {92, 83, 95, 71, 94}
Dim r(5)
'排序前資料輸出
For i = 0 To 4
str1 = str1 & a(i) & " "
Next
MsgBox(str1)
'排名排序
For j = 0 To 4
r(j) = 1
For i = 0 To 4
If a(j) < a(i) Then r(j) = r(j) + 1
Next
Next
'排序後資料輸出
str1 = ""
For i = 0 To 4
str1 = str1 & a(i) & " " & "名次:" & r(i) & vbNewLine
Next
MsgBox(str1)
End Sub
End Class
選擇排序
Public Class Form1
Dim str1 = ""
Dim i, j
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'原始資料
Dim a() = {2, 3, 5, 1, 4}
'排序前資料輸出
For i = 0 To 4
str1 = str1 & a(i) & " "
Next
MsgBox(str1)
'選擇排序 - 由小至大
For j = 0 To 3
For i = j + 1 To 4
If a(j) > a(i) Then
Dim t = a(j)
a(j) = a(i)
a(i) = t
End If
Next i
Next j
'排序後資料輸出
str1 = ""
For i = 0 To 4
str1 = str1 & a(i) & " "
Next
MsgBox(str1)
End Sub
End Class
氣泡排序
Public Class Form1
Dim i, j
Dim str1 = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'原始資料
Dim a() = {3, 2, 5, 1, 4}
'排序前資料輸出
For i = 0 To 4
str1 = str1 & a(i) & " "
Next i
MsgBox(str1)
'排序
For j = 0 To 3
For i = 0 To 3
If a(i) > a(i + 1) Then
Dim t = a(i)
a(i) = a(i + 1)
a(i + 1) = t
End If
Next i
Next j
'排序後資料輸出
str1 = ""
For i = 0 To 4
str1 = str1 & a(i) & " "
Next i
MsgBox(str1)
End Sub
End Class
2008年12月19日 星期五
求第1大、第2大值 ... -- 氣泡排序基礎
'求第1大、第2大值 ... -- 氣泡排序基礎
Public Class Form1
Dim i, j
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a() = {2, 5, 4, 1, 3}
For j = 1 To 4
For i = 0 To 3
If a(i) > a(i + 1) Then
Dim t = a(i)
a(i) = a(i + 1)
a(i + 1) = t
End If
Next
Next j
For i = 0 To 4
MsgBox(a(i))
Next i
End Sub
End Class