標籤雲
2010年11月5日 星期五
二進位列舉應用
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str1 = ""
Dim str2 = ""
Dim a() = {0, 1}
Dim e1, e2, e3, e4
For Each e4 In a
For Each e3 In a
For Each e2 In a
For Each e1 In a
str1 = str1 & e4 & e3 & e2 & e1 & vbNewLine
str2 = str2 & IIf(e4 = 1, "(肉)", "肉") & IIf(e3 = 1, "(菜)", "菜") & IIf(e2 = 1, "(蛋)", "蛋") & IIf(e1 = 1, "(果)", "果") & vbNewLine
Next
Next
Next
Next
MsgBox(str1 & vbNewLine & str2)
End
End Sub
End Class
二進位列舉
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str1 = ""
Dim a() = {0, 1}
Dim e1, e2, e3, e4
For Each e4 In a
For Each e3 In a
For Each e2 In a
For Each e1 In a
str1 = str1 & e4 & e3 & e2 & e1 & vbNewLine
Next
Next
Next
Next
MsgBox(str1)
End Sub
End Class
2010年10月6日 星期三
Joseph Problem
'Joseph Problem
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim aL As New ArrayList
Dim aLB As New ArrayList
Dim aStr = "123456789ABCDE"
For i = 1 To aStr.Length
aL.Add(Mid(aStr, i, 1))
Next
Dim k = 3
Dim ic = 0 + k - 1
While aL.Count > 1
ic = ic Mod aL.Count
aLB.Add(aL.Item(ic))
aL.RemoveAt(ic)
ic = ic + k - 1
End While
MsgBox(aL.Item(0))
End Sub
End Class
2010年9月10日 星期五
93q6
Public Class Form1
Private Sub Form1_Load(ByVal sender1 As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i, j
Dim str2 = ""
Dim str1 = ""
Dim a(51, 51)
a(1, 0) = 0 : a(1, 1) = 1 : a(1, 2) = 0
a(2, 0) = 0 : a(2, 1) = 1 : a(2, 2) = 2 : a(2, 3) = 1 : a(2, 4) = 0
str1 = 1
str2 = str2 & str1 & vbNewLine
str1 = 1 & ", " & 2 & ", " & 1
str2 = str2 & str1 & vbNewLine
For j = 3 To 10
str1 = ""
a(j, 1) = 1
str1 = str1 & 1 & ", "
For i = 2 To j
a(j, i) = a(j - 1, i - 1) + a(j - 1, i)
str1 = str1 & a(j, i) & ", "
Next
a(j, i) = 1
str1 = str1 & 1
str2 = str2 & str1 & vbNewLine
Next j
MsgBox(str2)
End Sub
End Class
2009年6月25日 星期四
97技藝競賽第8題 -- VB2008版
Public Class Form1
Dim r1Str = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test8.txt")
Dim recArray() = Split(fileContents, vbNewLine)
For k = 0 To recArray.Length - 1
proc_one_rec(recArray, k)
Next
'MsgBox(r1Str)
My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result8.txt", r1Str, False)
End
End Sub
Sub proc_one_rec(ByVal recArray, ByVal k)
Dim s = recArray(k)
Dim n = Math.Ceiling(s.length ^ 0.5)
Dim a(n + 1, n + 1) As String
For i = 0 To n + 1
For j = 0 To n + 1
If i = 0 Or i = n + 1 Or j = 0 Or j = n + 1 Then
a(i, j) = "X"
Else
a(i, j) = "#"
End If
Next
Next
Dim sindex = 1
Dim x = 1
Dim y = 1
Dim dx = 1
Dim dy = 0
Dim dirN = 0
Dim prex = x
Dim prey = y
While sindex <= s.length
'非空位處理,退回原位置,轉向,前進至下一位置
If a(x, y) <> "#" Then
x = prex
y = prey
dirN = dirN + 1
dirN = dirN Mod 4
If dirN = 0 Then
dx = 1
dy = 0
End If
If dirN = 1 Then
dx = 0
dy = 1
End If
If dirN = 2 Then
dx = -1
dy = 0
End If
If dirN = 3 Then
dx = 0
dy = -1
End If
x = x + dx
y = y + dy
End If
a(x, y) = Mid(s, sindex, 1)
'保留上次位置
prex = x
prey = y
'取得下次位置,下一個字元
x = x + dx
y = y + dy
sindex = sindex + 1
End While
'一次結果
Dim rStr = ""
For y = 1 To n
For x = 1 To n
rStr = rStr + a(x, y)
Next
rStr = rStr & vbNewLine
Next
rStr = Replace(rStr, "#", " ")
r1Str = r1Str & rStr & vbNewLine
End Sub
End Class
97技藝競賽第8題
Problem8:文字繞圈圈(15%)
已知有一個字串,請找出可以將此字串填入的最小正方形,而填入的方式是由正方形的左上角開始,依順時針蚊香方向由外向內填入。
輸入說明:
每組輸入包含一列字串,字串內的字元皆由英文字母及數字所組成,字串的長度最長不會超過1000個字元。
輸出說明:
對於每組測試資料,輸出填入最小正方形後的結果,每組測試資料以一列空白隔開。
輸入範例:
0123456789ABCDE
111111111111111122222222
輸出範例:
0123
BCD4
A E5
9876
11111
12221
12 21
12221
11111
Public Class Form1
Dim i, j, ri
Dim r1Str = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test8.txt")
Dim recArray() = Split(fileContents, vbNewLine)
For ri = 0 To recArray.Length - 1
proc_one_rec(recArray)
Next
'MsgBox(r1Str)
My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result8.txt", r1Str, False)
End
End Sub
Sub proc_one_rec(ByVal recArray)
Dim s = recArray(ri)
Dim n = Math.Ceiling(s.length ^ 0.5)
Dim a(n + 1, n + 1) As String
For i = 0 To n + 1
For j = 0 To n + 1
If i = 0 Or i = n + 1 Or j = 0 Or j = n + 1 Then
a(i, j) = "X"
Else
a(i, j) = "#"
End If
Next
Next
Dim sindex = 1
Dim x = 1
Dim y = 1
Dim dx = 1
Dim dy = 0
Dim dirN = 0
Dim prex = x
Dim prey = y
While sindex <= s.length
'非空位處理,退回原位置,轉向,前進至下一位置
If a(x, y) <> "#" Then
x = prex
y = prey
dirN = dirN + 1
dirN = dirN Mod 4
If dirN = 0 Then
dx = 1
dy = 0
End If
If dirN = 1 Then
dx = 0
dy = 1
End If
If dirN = 2 Then
dx = -1
dy = 0
End If
If dirN = 3 Then
dx = 0
dy = -1
End If
x = x + dx
y = y + dy
End If
a(x, y) = Mid(s, sindex, 1)
'保留上次位置
prex = x
prey = y
'取得下次位置,下一個字元
x = x + dx
y = y + dy
sindex = sindex + 1
End While
'一次結果
Dim rStr = ""
For y = 1 To n
For x = 1 To n
rStr = rStr + a(x, y)
Next
rStr = rStr & vbNewLine
Next
rStr = Replace(rStr, "#", " ")
r1Str = r1Str & rStr & vbNewLine
End Sub
End Class
2008年11月28日 星期五
技藝競竇95年模擬試題參考答案 Q2 (過橋問題)
' 二、過橋問題 (15%)
' 有n個人想要在晚上過橋,橋上每次最多只能容許兩個人行走。由於全部只有一支手電筒,
' 所以每次兩個人拿著手電筒過橋後,必須有一人再把手電筒拿回來,這樣後面的人才能繼續過橋。
' 每個人走路的速度不同,過橋所需的時間也因此不同。而每次過橋的那兩個人,其花費的時間以較慢的那個人計算
' (走的快的當然要等走的慢的,因為只有一支手電筒)。你的任務是寫一個程式,安排這n個人過橋,
' 並使得總共花費的時間最少。
' 輸入規範
' 輸入檔案的第一列有一個正整數,代表以下有多少組測試資料。每組測試資料的第一列有1個整數n,
' 代表要過橋的人數(最多不會超過1000人)。接下來的n列,每列有1個整數,代表這n個人過橋所需的時間(秒),
' 這些時間均不會超過100秒。
' 輸入的第一列與第一組測試資料之間,以及各組測試資料之間均有一空白列(請參考輸入範例)。
' 輸出規範
' 每組測試資料輸出的第一列為一個整數,代表這組中n個人過橋所需的最少時間。
' 接下來的列為達到此最少時間的過橋方式。每列有1個或2個整數代表過橋的人
' (每個人以其過橋所需的時間代表。雖然可能有2人過橋時間相同,但那並不會影響結果)。
' 請注意,過橋的順序是去、回交替的,因為需有一人把手電筒帶回。
' 以第一組輸出為例說明:最少需17秒才能讓這4個人過橋。
' 方式為:1秒、2秒的人先過橋,1秒的回來,5秒、10秒的過橋,2秒的回來,最後1秒、2秒的過橋,
' 所以總共的時間為:2+1+10+2+2=17。
' 如果有不止一種方式可以達到最少時間,輸出任何一種均可。輸出資料的組間亦請空一列。
' 輸入範例(test2.txt)
' 2
' 4
' 1
' 2
' 5
' 10
' 4
' 1
' 98
' 99
' 100
' 輸出範例(result2.txt)
' 17
' 1 2
' 1
' 5 10
' 2
' 1 2
' 299
' 1 100
' 1
' 1 99
' 1
' 1 98
' 過橋問題不限定人數版
Public Class Form1
Dim dlist As New ArrayList
Dim slist As New ArrayList
Dim resultStr = ""
Dim resultStr1 = ""
Dim total = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test2.txt")
Dim recArray = Split(fileContents, vbNewLine)
Dim ternN = Val(recArray(0))
Dim recIndex = 2
For j As Integer = 1 To ternN
Dim dataN = Val(recArray(recIndex))
For i As Integer = 1 To dataN
slist.Add(Val(recArray(recIndex + i)))
Next
slist.Sort()
total = 0
resultStr = ""
bridge(slist, dlist, slist.Count)
resultStr = total & vbNewLine & resultStr
resultStr1 = resultStr1 & resultStr & vbNewLine
recIndex = recIndex + dataN + 1 + 1
Next
'MsgBox(resultStr1)
My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result2.txt", resultStr1, False)
End
End Sub
Sub bridge(ByVal slist, ByVal dlist, ByVal n)
If n = 2 Then
slist.sort()
resultStr = resultStr & slist(0) & " " & slist(1) & vbNewLine
total = total + IIf(slist(0) > slist(1), slist(0), slist(1))
dlist.Add(slist(0))
dlist.Add(slist(1))
slist.Remove(slist(1))
slist.Remove(slist(0))
Else
slist.Sort()
If n = 3 And (slist(2) - slist(1)) > (slist(1) - slist(0)) Then
slist.Reverse()
resultStr = resultStr & slist(1) & " " & slist(0) & vbNewLine
Else
resultStr = resultStr & slist(0) & " " & slist(1) & vbNewLine
End If
total = total + IIf(slist(0) > slist(1), slist(0), slist(1))
dlist.Add(slist(0))
dlist.Add(slist(1))
slist.Remove(slist(1))
slist.Remove(slist(0))
dlist.Sort()
resultStr = resultStr & dlist(0) & vbNewLine
total = total + dlist(0)
slist.Add(dlist(0))
dlist.Remove(dlist(0))
bridge(slist, dlist, n - 1)
End If
End Sub
End Class
技藝競竇97年模擬試題參考答案 Q7
' Problem7:奥步戰術(17%)
' 在黑暗算法界中,使用奧步解題似乎已經漸漸成為主流。雖然使用奧步將漸漸使人走向魔路,最後被內心的虛無吞
' 噬,不過這不是今天的問題。考慮在某個考試中,有n 道題目,而總答題時間為T。對於每題都只有三種可能:
' 1. 正解能得到全對的分數(得2 分)
' 2. 奧步能拿到半對(得1 分)
' 3. 放棄的話當然就沒分囉(0 分)
' 而對每題來說,要達到這三種分數所需花的時間皆不同,所有題目拿0 分都不用花費時間;在題目i 使用奧步拿半
' 對所需時間為Hi,要寫正解所需時間為Ci ,其中對於任何題目i,必有滿足0' 試問:在時間T 內,用最佳的答題方式,最多可以拿幾分?
' 輸入說明:
' 輸入檔第一行說明有幾組測試資料,第二行有兩個整數n 和T,分別代表有幾題,以及總作答時間。接下來n 行每
' 行有兩個整數Ci 和Hi,代表第i 題寫正解需要時間Ci,寫奧步需要時間Hi。其中:
' 題目總數n≤100000
' 答題所需時間1≤Hi,Ci≤1000000
' 總作答時間0
' 每個測試範例請輸出一個整數,代表最大得分。
' 輸入範例:
' 2
' 5 12
' 4 3
' 6 2
' 5 3
' 4 3
' 5 2
' 4 10
' 5 3
' 6 5
' 3 1
' 4 3
' 輸出範例:
' 6
' 5
Structure recData
Dim c As Integer
Dim h As Integer
Dim cR As Single
Dim hR As Single
Dim isC As Boolean
Dim isUsed As Boolean
End Structure
Public Class Form1
' Dim debugStr = ""
Dim resultStr = ""
Dim score = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim a(100000) As recData
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test7.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim recN = Val(recArray(0))
Dim recIndex = 1
For j As Integer = 1 To recN
'read data for one turn
Dim dataArray = Split(recArray(recIndex), " ")
Dim dataN = Val(dataArray(0))
Dim t = Val(dataArray(1))
For i = 1 To dataN
dataArray = Split(recArray(recIndex + i))
a(i).c = Val(dataArray(0))
a(i).h = Val(dataArray(1))
a(i).cR = 2 / a(i).c
a(i).hR = 1 / a(i).h
a(i).isC = True
a(i).isUsed = False
Next
'proc one turn
Dim c = 1
While t > 0 And c <= dataN
'find the max
Dim pMax As Single = 0
Dim maxIndex = 0
For i3 = 1 To dataN
If a(i3).cR > pMax And a(i3).isUsed = False Then
pMax = a(i3).cR
maxIndex = i3
End If
Next
For i3 = 1 To dataN
If a(i3).hR > pMax And a(i3).isUsed = False Then
pMax = a(i3).hR
maxIndex = i3
a(i3).isC = False
End If
Next
'accmulate score and subtract the time
If a(maxIndex).isC = True Then
t = t - a(maxIndex).c
score = score + 2
'debugStr = debugStr & a(maxIndex).c & a(maxIndex).h & "C" & vbNewLine
Else
t = t - a(maxIndex).h
score = score + 1
' debugStr = debugStr & a(maxIndex).c & a(maxIndex).h & "H" & vbNewLine
End If
a(maxIndex).isUsed = True
c = c + 1
End While
' MsgBox(debugStr)
resultStr = resultStr & score & vbNewLine
' clear data for next turn
ReDim a(100000)
score = 0
recIndex = recIndex + dataN + 1
Next
MsgBox(resultStr)
'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result7.txt", resultStr, False)
End
End Sub
End Class
技藝競竇97年模擬試題參考答案 Q6
' Problem6:中文大寫數字(13%)
' 我們在金融機構填寫金額時使用的不是阿拉伯數字,而是中文的大寫數字。請寫一個程式將數字轉換為中文大寫數
' 字。
' 標準大寫寫法如下:零、壹、貳、參、肆、伍、陸、柒、捌、玖、拾、佰、仟、萬、億
' 輸入說明:
' 整數數字n (0 ≤ n ≤ 2147483647)。
' 輸出說明:
' 文字字串,遇到10 時,請輸出『壹拾』。
' 輸入範例:
' 12345
' 10200
' 輸出範例:
' 壹萬貳仟參佰肆拾伍
' 壹萬零貳佰
'
' 這一題只考慮測試資料會錯的很離譜
' 這個程式可以處理102345=>壹拾萬貳仟参佰肆拾伍情況
Public Class Form1
Dim resultStr = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim strB = "零壹貳參肆伍陸柒捌玖"
Dim strA = "0123456789"
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test6.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim recIndex = 0
While recArray(recIndex) <> ""
Dim strS = recArray(recIndex)
Dim strT = "" '暫存每次一筆記錄轉成之字串
Dim numT = Mid(strB, InStr(strA, Mid(strS, Len(strS), 1)), 1)
strT = numT
Dim level = 1 '單位
For i = Len(strS) - 1 To 1 Step -1
Dim strLevel = Choose(level, "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾")
numT = Mid(strB, InStr(strA, Mid(strS, i, 1)), 1)
If numT = "零" Then
If strLevel = "萬" Or strLevel = "億" Then
strT = numT & strLevel & strT
Else
strT = numT & strT
End If
Else
strT = numT & strLevel & strT
End If
level = level + 1
Next
'修飾strT
If Len(strT) > 3 Then
If Microsoft.VisualBasic.Left(strT, 3) = "壹拾零" Then
strT = Microsoft.VisualBasic.Left(strT, 2) & Mid(strT, 4, Len(strT) - 3)
End If
Else
strT = "壹拾"
End If
'改連續零為一個零
Dim pre = "零"
Dim strT1 = ""
For j = Len(strT) To 1 Step -1
If Not (Mid(strT, j, 1) = "零" And pre = "零") Then
strT1 = Mid(strT, j, 1) & strT1
End If
pre = Mid(strT, j, 1)
Next
strT = strT1
'處理零萬狀況
If Microsoft.VisualBasic.Right(strT, 2) = "零萬" Then
strT = Microsoft.VisualBasic.Left(strT, Len(strT) - 2)
End If
If InStr(strT, "拾零萬") <> 0 Then
strT = Replace(strT, "拾零萬", "拾萬")
End If
'處理零狀況
If recArray(recIndex) = "0" Then strT = "零"
resultStr = resultStr & strT & vbNewLine
recIndex = recIndex + 1
End While
MsgBox(resultStr)
' My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result5.txt", resultStr, False)
End
End Sub
End Class
技藝競竇97年模擬試題參考答案 Q5
' Problem5:糊塗情報員(16%)
' 有一位間諜,依他所屬情報單位要求編碼的方式,將他所收集到情報全部編成數字碼。但他認為這樣還是不夠安全,
' 因此他再將這些數字字串,隨意切割成好幾個整數,然後將每個整數用一個數學算式來表示。這些算式只用了加、減、
' 乘三種運算子,而且每個運算元都是正整數。最後,他為了讓他自己更為心安,他將整個密碼分成兩本密碼簿儲存。密
' 碼本A 存放這些數學算式,但他將算式內的所有括號全部拿掉,然後再將這些拿掉的括號資訊記錄在密碼本B 裡面。
' 過了不久,這個間諜發現他把密碼本B 弄丟了,再加上他的記憶力不好,很多情報內容根本記不得,所以現在沒了
' 密碼本B 幾乎束手無策。在不得已的情況下,他的情報單位派了幾位心理與腦神經專家詢問他,希望能喚起他腦海內的
' 記憶。這些專家試了好幾天,用盡各種辦法後,終於承認他的記憶力果真很差,怎麼也問不出情報內容。倒是心理學專
' 家有一發現,即這位情報員在寫密碼算式時,傾向於將括號加在那些會讓算式得最大值的位置。例如5*7+2 這個算式,
' 有兩種括法:((5*7)+2)以及(5*(7+2)),第二種括法所得的值較大。請寫一程式,算出這些算式的可能最大值。
' 輸入說明:
' 每一筆輸入資料為一行算式,運算子只有三種,即一般的加、減、乘三種二元運算子,分別以符號' +' 、' -' 、' *'
' 表示。每一個運算元都是一個正整數( ≤ 100),運算元和運算子之間不會有空白,一行算式不會有超過50 個運算元。
' 輸出說明:
' 相對於每一輸入算式,輸出所有可能運算結果的最大值。該值都會是一個正整數,而且不會超過2147483647。
' 輸入範例:
' 5*7+2
' 6*3-9*3
' 5+2-7*2-3
' 範例輸出:
' 45
' 27
' 14
Public Class Form1
Dim resultStr = ""
Dim str1 As String
Dim ans As Integer
Dim ans1 As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test5.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim recIndex = 0
While recArray(recIndex) <> ""
Dim str1 = recArray(recIndex)
proc_one_rec(str1)
recIndex = recIndex + 1
End While
MsgBox(resultStr)
'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result5.txt", resultStr, False)
End
End Sub
Sub proc_one_rec(ByVal str1)
'由左而右
ans = Val(Mid(str1, 1, 1))
listStr(str1, 2)
'由右而左
ans1 = Val(Mid(str1, Len(str1), 1))
listStr1(str1, Len(str1) - 1)
Dim ansBig = IIf(ans > ans1, ans, ans1)
resultStr = resultStr & ansBig & vbNewLine
'MsgBox(ansBig & " " & ans & " " & ans1)
End Sub
Sub listStr(ByVal str1, ByVal n)
If n = Len(str1) - 1 Then
Dim op = Mid(str1, n, 1)
Dim opN = Mid(str1, n + 1, 1)
ans = Microsoft.VisualBasic.Switch(op = "+", ans + opN, op = "-", ans - opN, op = "*", ans * opN)
Else
Dim op = Mid(str1, n, 1)
Dim opN = Mid(str1, n + 1, 1)
ans = Microsoft.VisualBasic.Switch(op = "+", ans + opN, op = "-", ans - opN, op = "*", ans * opN)
listStr(str1, n + 2)
End If
End Sub
Sub listStr1(ByVal str1, ByVal n)
If n = 2 Then
Dim op = Mid(str1, n, 1)
Dim opN = Mid(str1, n - 1, 1)
ans1 = Microsoft.VisualBasic.Switch(op = "+", opN + ans1, op = "-", opN - ans1, op = "*", opN * ans1)
Else
Dim op = Mid(str1, n, 1)
Dim opN = Mid(str1, n - 1, 1)
ans1 = Microsoft.VisualBasic.Switch(op = "+", opN + ans1, op = "-", opN - ans1, op = "*", opN * ans1)
listStr1(str1, n - 2)
End If
End Sub
End Class
技藝競竇97年模擬試題參考答案 Q4
' Porblem4:百貨公司打折程式(12%)
' 豪慷慨百貨公司週年慶的打折策略,吸引了許多客人上門,因此公司決定再回饋客戶,當客戶消費超過2000 元時
' 打7 折,消費超過5000 元時打6 折,消費超過10000 元時打55 折。請幫該公司寫出一個新的收銀台程式,輸入顧客購
' 買總金額n 後,計算顧客實際需付的錢。
' 輸入說明:
' 購買金額n
' 輸出說明:
' 實付金額
' 輸入範例:
' 3000
' 6000
' 12000
' 輸出範例:
' 2100
' 3600
' 6600
Public Class Form1
Dim resultStr = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test4.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim recIndex = 0
While recArray(recIndex) <> ""
Dim amount = recArray(recIndex)
Dim finalAmount = amount
If amount > 2000 Then
finalAmount = amount * 0.7
End If
If amount > 5000 Then
finalAmount = amount * 0.6
End If
If amount > 10000 Then
finalAmount = amount * 0.55
End If
resultStr = resultStr & finalAmount & vbNewLine
recIndex = recIndex + 1
End While
MsgBox(resultStr)
'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result4.txt", resultStr, False)
End
End Sub
End Class
技藝競竇97年模擬試題參考答案 Q3
' Problem3:基礎排序(13%)
' 給一堆數字, 把他們從小到大排序好。
' 輸入說明:
' 每組測試資料共有兩行,第一行的數字n 為有幾個數字要排序,第二行則有n 個整數(n ≤1000),其餘整數皆於-10000
' 到10000 之間,測試資料中包含多組測試,當排序個數為0 時結束。
' 輸出說明:
' 輸出已排序好的數列,每個數字之間請用一個空白隔開。
' 輸入範例:
' 5
' 5 4 3 2 1
' 5
' -1 -2 -3 -4 -5
' 0
' 輸出範例:
' 1 2 3 4 5
' -5 -4 -3 -2 -1
Public Class Form1
Dim resultStr = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test3.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim recIndex = 0
While recArray(recIndex) <> "0"
Dim N = Val(recArray(recIndex))
Dim dataArray() = Split(recArray(recIndex + 1))
For i = 0 To N - 1 - 1
For j = i + 1 To N - 1
If Val(dataArray(i)) > Val(dataArray(j)) Then
Dim tem = dataArray(i)
dataArray(i) = dataArray(j)
dataArray(j) = tem
End If
Next
Next
For i = 0 To N - 1
resultStr = resultStr & dataArray(i) & " "
Next
resultStr = resultStr & vbNewLine
recIndex = recIndex + 2
End While
MsgBox(resultStr)
'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result3.txt", resultStr, False)
End
End Sub
End Class
技藝競竇97年模擬試題參考答案 Q2
' Problem2:惱人的零錢(15%)
' 東東在買東西付帳,總是習慣直接從錢包中拿鈔票付帳,而懶得掏出硬幣來。久而久之,錢包裡面累積了許多硬幣,
' 簡直重得不得了,所以東東終於受夠了!因此,她決定趁著今天買東西的時候,想辦法盡量減輕負擔。於是東東開始盤
' 算要怎樣湊出足夠的硬幣,才能讓付出去的硬幣個數越多越好。同時,目前這家店的老闆人很好,不論客人給他多少硬
' 幣,他都一定會用最少的硬幣找錢給客人。所以,當東東走到櫃台結帳時,東東想到自己如果多付一些硬幣讓老闆找錢,
' 說不定可以讓自己的錢包更輕!因此,東東開始煩惱到底要怎麼給錢,才能夠盡量「用掉」最多的硬幣呢(所謂的「用
' 掉」的硬幣個數,指的是拿出去的硬幣數,扣掉老闆找回來的硬幣數)?可惜的是,東東的算術一向不太靈光,因此希
' 望你能幫忙他解決這個煩惱。
' 輸入說明:
' 輸入資料的第一行是一個整數n,代表共有n 筆測試資料。接下來每筆測試資料有3 行:第1 行的數字C 表示要
' 買的東西的價格。第2 行有5 個數字p1 p5 p10 p20 p50,分別是東東錢包裡面一元、五元、十元、二十元和五十元硬幣
' 的個數。第3 行有5 個數字q1 q5 q10 q20 q50,是老闆所擁有的一元、五元、十元、二十元和五十元硬幣的個數。每
' 筆測試資料的所有數字都在0 到10000 之間;同一行的數字之間會用一個空白隔開。你可以假設東東身上的錢足夠來
' 購買該商品,而且至少有一種付錢的方法使得老闆可以找得開(如果需要找錢的話)。因為老闆和東東很不幸地很碰巧
' 地一張鈔票都沒有,請不要問說為什麼不能換成大鈔。
' 輸出說明:
' 你的輸出資料應該有n 行,分別對應到n 筆輸入的測試資料。每一行要輸出一個數字表示東東付完帳之後,剩餘
' 的硬幣總數。
' 輸入範例:
' 2
' 25
' 10 3 2 1 3
' 0 0 0 0 0
' 25
' 0 3 2 2 3
' 1 1 1 1 1
' 輸出範例:
' 6
' 4
Public Class Form1
Dim resultStr = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test2.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim groupN = Val(recArray(0))
Dim recIndex = 1
For j = 1 To groupN
Dim price = recArray(recIndex)
Dim eeArray() = Split(recArray(recIndex + 1))
Dim bossArray() = Split(recArray(recIndex + 2))
Dim p1 = Val(eeArray(0))
Dim p5 = Val(eeArray(1))
Dim p10 = Val(eeArray(2))
Dim p20 = Val(eeArray(3))
Dim p50 = Val(eeArray(4))
Dim q1 = Val(bossArray(0))
Dim q5 = Val(bossArray(1))
Dim q10 = Val(bossArray(2))
Dim q20 = Val(bossArray(3))
Dim q50 = Val(bossArray(4))
Dim pq1 = p1 + q1
Dim pq5 = p5 + q5
Dim pq10 = p10 + q10
Dim pq20 = p20 + q20
Dim pq50 = p50 + q50
Dim amount = p1 * 1 + p5 * 5 + p10 * 10 + p20 * 20 + p50 * 50
Dim remain = amount - price
Dim n50 = 0
While remain >= 50 And pq50 >= 1
n50 = n50 + 1
remain = remain - 50
pq50 = pq50 - 1
End While
Dim n20 = 0
While remain >= 20 And pq20 >= 1
n20 = n20 + 1
remain = remain - 20
pq20 = pq20 - 1
End While
Dim n10 = 0
While remain >= 10 And pq10 >= 1
n10 = n10 + 1
remain = remain - 10
pq10 = pq10 - 1
End While
Dim n5 = 0
While remain >= 5 And pq5 >= 1
n5 = n5 + 1
remain = remain - 5
pq5 = pq5 - 1
End While
Dim n1 = 0
While remain >= 1 And pq1 >= 1
n1 = n1 + 1
remain = remain - 1
pq1 = pq1 - 1
End While
resultStr = resultStr & (n50 + n20 + n10 + n5 + n1) & vbNewLine
recIndex = recIndex + 3
Next j
MsgBox(resultStr)
'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result2.txt", resultStr, False)
End
End Sub
End Class
技藝競竇97年模擬試題參考答案 Q1
' Problem1:字數統計(14%)
' 雷迪雅被老師要求寫一份為數2000 字的報告,可是雷迪雅胸無點墨、才疏學淺,常常寫了一整個下午還不到一頁,
' 因此他每寫一段時間就開始數自己已經寫了多少個字,可是常常這樣數也是會累的,於是他希望能有一個自動字數統計
' 程式。
' 有了字數統計程式,就可以自動統計好一篇文章總共有幾行、有幾個字、有幾個字元。行被定義為用換行字元隔開
' 的連續字元,字被定義為用空白、TAB 或換行字元所隔開的連續字元,而字元除了一般可見的字元還包括TAB 字元和
' 空白字元(注意不包含換行字元)。
' 身為雷迪雅好朋友的你,常常受到他的照顧,正所謂吃人嘴軟,拿人手短,如今雷迪雅遇到了這個難題,請你義不
' 容辭地寫一個程式幫他吧!
' 輸入說明:
' 輸入檔第一行說明有幾組測試資料,第二行開始即為第一筆測試資料,每行不會超過1024 個字元,每組測試資料
' 中間用五個連續等號' =' 的一行來作分隔。每組測試資料之中絕不會有五個連續等號' =' 出現。
' 輸出說明:
' 每組測試資料輸出一行,每行有三個數字,分別代表一組測試資料中有幾行,幾個字和幾個字元,每個數字之間請
' 用一個空白隔開。
' 輸入範例:
' 2
' This is a sample input.
' HelloWorld!!
' =====
' The speech by Hunyak, translated, is:
' "What am I doing here?
' They say, the famous Hungarian police,
' that I held down my husband and chopped off his head.
' But I didn' t do it, I am not guilty.
' I can' t believe that Uncle Sam says I did it.
' They say I didit, but really I didn' t."
' 輸出範例:
' 2 7 36
' 8 55 270
Public Class Form1
Dim resultStr = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
Dim fileContents As String
fileContents = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\Test1.txt")
Dim recArray() = Split(fileContents, vbNewLine)
Dim articleN = Val(recArray(0))
Dim recIndex = 1
Dim lineCtr = 0
Dim wordCtr = 0
Dim charCtr = 0
While recIndex <= UBound(recArray)
If recArray(recIndex) <> "=====" And recIndex <> UBound(recArray) Then
lineCtr = lineCtr + 1
Dim wordArray() = Split(recArray(recIndex))
If recArray(recIndex) <> "" Then
wordCtr = wordCtr + UBound(wordArray) + 1
End If
charCtr = charCtr + Len(recArray(recIndex))
Else
resultStr = resultStr & lineCtr & " " & wordCtr & " " & charCtr & vbNewLine
lineCtr = 0
wordCtr = 0
charCtr = 0
End If
recIndex = recIndex + 1
End While
MsgBox(resultStr)
'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\result1.txt", resultStr, False)
End
End Sub
End Class