二信高中資處107年技藝競賽培訓選手模擬賽1(2018/3/7~3/14)
共六題,皆讀兩個檔in1.txt、in2.txt,輸出一個out.txt兩個輸入檔的輸出間空一列
測資分別放於p11~p16資料夾內,已提供讀檔寫檔之範例程式碼如所附 p00.txt
p00.txt
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
FileOpen(1, "in1.txt", 1)
FileOpen(2, "in2.txt", 1)
FileOpen(3, "out.txt", 2)
For fn = 1 To 2
If fn = 2 Then PrintLine(3)
Dim n As Short = LineInput(fn)
For k = 1 To n
Dim s As String = LineInput(fn)
PrintLine(3, fnxx(s))
Next
Next
End
End Sub
Function fnxx(ByVal s As String) As String
fnxx = ""
Rem 解題處
End Function
'若資料以空格隔開,可能需要將多個空白 取代為 單空白
Sub ttrim(ByRef s As String, ByVal a As String, ByVal b As String)
While (InStr(s, a) > 0)
s = s.Replace(a, b)
End While
End Sub
End Class
P11:第1題 – 找出隱藏的質數
每個輸入檔中第一列為一個正整數N(1 ≤ N≤ 10)
接著有N列,每列一個正整數X(1 ≤ X ≤ 1030)
找出X其可能藏有的質數,可以重疊;例237藏有的質數有2,3,7,23,37共有五個質數。
輸出第1個為所藏的質數個數 { 只找 < 109的質數),接著一個冒號後接著由小至大列出所有質數,質數間以逗號隔開,輸出例如右「5:2,3,7,23,37」
In1.txt
5
237
1329
24681357
123456789
9876543201
|
Out.txt
5:2,3,7,23,37
4:2,3,13,29
5:2,3,5,7,13
9:2,3,5,7,23,67,89,4567,23456789
7:2,3,5,7,43,43201,76543
7:2,3,5,7,23,79,2357
10:2,3,5,7,31,53,97,809,246809,6809753
31:2,3,5,7,13,29,37,47,59,71,73,137,359,373,503,593,
3593,5939,8713,9871,13759,39847,62987,75037,98713,
362987,475037,939847,4750373,8475037,98475037
11:3,5,7,37,43,53,643,757,7757,8664377,866437757
6:2,3,5,7,67,503
|
續
續
|
In2.txt
5
23579
2468097531
935939847503736298713759632
5330866437757
85036267
|
P12:第2題 – 十進位轉X進位
每個輸入檔中第一列為一個正整數N(1 ≤ N≤ 10)
接著有N列,每列兩個非負整數D,X以逗號隔開。(0 ≤D≤ 1015) (2 ≤X≤ 16)
將十進位整數D轉成 X進位,10~15分別以大寫ABCDEF代替
In1.txt
5
237,3
1329,5
24681357,7
123456789,12
9876543201,15
|
Out.txt
22210
20304
416534211
35418A99
3CC123286
301055
30443C553
4481788305478580
1001101100100110000011001100111111001111101
5118CEB
|
|
In2.txt
5
23579,6
2468097531,13
935939847503736,9
5330866437757,2
85036267,16
|
P13:身份證檢查碼
每個輸入檔中第一列為一個正整數N(1 ≤ N≤ 10)
接著有N列,每列為一個阿華民國的身份證前9碼。
阿華民國的身份證由兩個大寫英文字及八個數字組成,其編號為
E1 E2 D1 D2 D3 D4 D5 D6 D7 D8,其中E1 E2 英文字母對應如下表
A
|
C
|
E
|
F
|
I
|
N
|
R
|
V
|
Y
|
11
|
12
|
13
|
14
|
15
|
26
|
37
|
48
|
59
|
當由上表中,找到其對應的兩位數字後,則 E1 對應的數字的十位數為 X1 , 個位數為 Y1 , E2 對應的數字十位為 X2 , 個位數為 Y2, 依下列公式計算Z
Z=9*(X1+X2) + 8*(Y1+Y2)
+7*D1+6*D2+5*D3+4*D4+3*D5+2*D6+D7
而D8=10-(Z 除 10 的餘數)的個位數取得,稱為檢查碼。
給身份證前9碼,印出完整的身份證例如AC1234567則X1=1,Y1=1,X2=1,Y2=2,
Z=9*(1+1)+8*(1+2)+7*1+6*2+5*3+4*4+3*5+2*6+1*7=126則D8=4
In1.txt
3
AC1234567
FN5678903
RV7654321
|
Out.txt
AC12345674
FN56789035
RV76543217
VY84565769
YA87748594
IE98753567
|
|
In2.txt
3
VY8456576
YA8774859
IE9875356
|
P14:分數四則運算
每個輸入檔中第一列為一個正整數N(1 ≤ N≤ 10)
接著有N列,每列為一個分數的算式字串: a|b㊉c|d
其中㊉是運算符號,可以是 + - * / 其中之一, 0 < a,b,c,d < 4*104的正整數
運算結果若分子絕對值大於等於分母,輸出為帶分數型式(與分數部份空1格),
若整除則不印分數部份,分數的部份化為最簡式,負號在最左方
In1.txt
2
3|2+5|4
2|3-14|4
In2.txt
2
2|3/4|5
2|3*6|4
|
Out.txt
2 3|4
-2 5|6
5|6
1
|
註:輸入與輸出的分子、分母間之直條 |
代表 | 與除號 / 有別
2與3|4之間空1格
負號在最左方
整除只印出整數部份
|
P15:計程車費
阿基市的計程車收費如下表
日間收費方式
|
|
夜間收費方式
|
起跳里程
|
起跳金額
|
續跳里程
|
續跳金額
|
延滯計費基準
|
延滯計費單價
|
|
起跳里程
|
起跳金額
|
續跳里程
|
續跳金額
|
延滯計費基準
|
延滯計費單價
|
時段
|
公尺
|
元
|
公尺
|
元
|
秒
|
元
|
|
公尺
|
元
|
公尺
|
元
|
秒
|
元
|
起
|
迄
|
1250
|
70
|
200
|
5
|
80
|
5
|
|
1250
|
70+20
|
200
|
5
|
80
|
5
|
23:00
|
06:00
|
每個輸入檔中第一列為一個正整數N(1 ≤ N≤ 10)
接著有N列,每列有三部份,第一個為里程D公里,二及三為起訖時間S,T且S<T
(0.001≤D≤100) 、S,T的格式為hh:mm:ss(0≤hh≤23、0≤mm,ss≤59)
只要有1秒鐘以上落在夜間即算,但剛有在起或訖為0秒不算夜間
續跳未滿200公尺無條件進位,延時未滿80秒無條件捨去。
例如1449公尺、1450公尺皆是 70元+續跳1次,而1451算續跳2次;
起訖共1~79秒皆不加延滯費,80~159秒算延1次。
In1.txt
3
1.46,08:00:10,08:05:23
2.345,10:11:22,10:12:41
1.23,22:59:01,23:03:05
|
Out.txt
95
100
105
120
225
120
|
|
In2.txt
3
2.56,08:00:10,08:05:23
5,05:59:59,06:11:22
3,06:00:00,06:02:39
|
|
A
|
B
|
C
|
D
|
E
|
F
|
1
|
|
(2)
|
|
(3)
|
|
|
2
|
(1)
|
|
|
|
(4)
|
|
3
|
|
|
▲
|
|
|
|
4
|
(8)
|
|
|
|
(5)
|
|
5
|
|
(7)
|
|
(6)
|
|
|
6
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
|
P16:騎士步
若騎士在C3處,他有8種走法(1)~(8),而且他的
優先順序一定是(1)開始除非前一種不能走才會選
下一種走法,給兩個座標,問走幾步會到達,若
無法到達則印出 -1{遇到起點或走過的點就停止}
每個輸入檔中第一列為一個正整數N(1 ≤ N≤ 10)
接著有N列,每列有三部份M,S,E
M為棋盤邊長(5≤ N≤
9),S,E為起訖座標:1英1數
In1.txt
3
6,C3,A2
5,C3,B4
5,C3,A3
In2.txt
2
5,C3,C3
6,B1,A1
|
Out.txt
1
14
-1
0
5
|
第15步A2,C2,D3,D5皆走過
|
參考解:
P11
Function fnxx(ByVal s As String) As String
Dim p As New ArrayList
Dim m As Integer = s.Length, k, r As Integer
For i = 1 To m
r = Math.Min(9, m + 1 - i)
For j = 1 To r
k = Mid(s, i, j) '從第i個起取j個
If (p.IndexOf(k) >= 0) Then Continue For '出現過的不重複
If isp(k) Then p.Add(k)
'Dim y As Boolean = isp(k)
' Debug.Print(k & ":" & y)
Next
Next
p.Sort() '排序
fnxx = p.Count & ":"
For i = 0 To p.Count - 1
If i > 0 Then fnxx &= ","
fnxx &= p(i)
Next
End Function
Function isp(ByVal k As Integer) As Boolean
If k < 2 Then Return False
For i = 2 To Math.Sqrt(k)
If k Mod i = 0 Then Return False
Next
Return True
End Function
P12
Const htbl As String = "0123456789ABCDEF"
Function fnxx(ByVal s As String) As String
fnxx = ""
Dim dat() = s.Split(",")
Dim d As Long = dat(0), x As Integer = dat(1)
Do Until d = 0
fnxx = Mid(htbl, d Mod x + 1, 1) & fnxx
d \= x
Loop
If fnxx = "" Then Return 0
End Function
P13
Function fnxx(ByVal s As String) As String
Dim tb1() As Char = {"A", "C", "E", "F", "I", "N", "R", "V", "Y"}
Dim tb2() As Integer = {11, 12, 13, 14, 15, 26, 37, 48, 59}
fnxx = ""
Dim c1 As Char = Mid(s, 1, 1), c2 As Char = Mid(s, 2, 1)
Dim X As Integer = tb2(Array.IndexOf(tb1, c1))
Dim Y As Integer = tb2(Array.IndexOf(tb1, c2))
Dim Z As Integer = 9 * (X \ 10 + Y \ 10) + 8 * (X Mod 10 + Y Mod 10)
For i = 3 To 9
Z += (10 - i) * Val(Mid(s, i, 1))
Next
fnxx = s & (10 - Z Mod 10) Mod 10
End Function
P14
Function fnxx(ByVal s As String) As String
'Dim x As Integer = s '若需轉為整數
fnxx = ""
' ========== 找 op , 及其位置 p
Dim op As Integer ' 0:+ 1:- 2:* 3:/
Dim p As Integer
p = InStr(s, "+") '加號的位置
If p > 0 Then
op = 0
Else
p = InStr(s, "-") '減號的位置
If p > 0 Then
op = 1
Else
p = InStr(s, "*") '乘號的位置
If p > 0 Then
op = 2
Else
p = InStr(s, "/") '減號的位置
op = 3
End If
End If
End If
' =====================
Dim s1 As String = Mid(s, 1, p - 1) '符號的左邊
Dim s2 As String = Mid(s, p + 1) '符號的右邊
Dim a, b, c, d As Integer '輸入值 0<a,b,c,d<4*10^4
s2n(s1, a, b) : s2n(s2, c, d)
Dim ac As Integer = a * c, ad As Integer = a * d
Dim bc As Integer = b * c, bd As Integer = b * d
' fnxx = a & "," & b & " " & op & " " & c & "," & d & " = " ''這行多印的
Select Case op
Case 0 : fnxx &= prt(ad + bc, bd) ' +
Case 1 : fnxx &= prt(ad - bc, bd) ' -
Case 2 : fnxx &= prt(ac, bd) ' *
Case 3 : fnxx &= prt(ad, bc) ' /
End Select
End Function
Sub s2n(ByVal s As String, ByRef x As Integer, ByRef y As Integer)
Dim dat() = s.Split("|")
x = dat(0) : y = dat(1)
End Sub
Function prt(ByVal p As Integer, ByVal q As Integer) As String
prt = ""
If p = 0 Then Return 0
If (p < 0) Then
prt = "-"
p = -p
End If
If p >= q Then
prt &= p \ q '帶或只有整
p = p Mod q
If p = 0 Then Return prt '只有整
prt &= " " '帶 加 分
End If
Dim g As Integer = gcd(p, q)
prt &= p / g & "|" & q / g
End Function
Function gcd(ByVal p As Integer, ByVal q As Integer) As Integer
Dim r As Integer = p Mod q
Do Until r = 0
p = q
q = r
r = p Mod q
Loop
Return q
End Function
P15
Const t1 As Integer = 6 * 60 * 60 ' 06:00:00
Const t2 As Integer = 23 * 60 * 60 ' 23:00:00
Function fnxx(ByVal s As String) As String
fnxx = ""
Dim dat() = s.Split(",")
Dim m As Integer = Val(dat(0)) * 1000 '公尺
Dim st1 As Integer = totsec(dat(1)), st2 As Integer = totsec(dat(2))
' fnxx = m & "," & st1 & "," & st2 & "=" & st2 - st1
Dim price As Integer = 70
If st1 < t1 Or st2 > t2 Then price += 20 '夜
m -= 1250
If (m < 0) Then m = 0
price += Math.Ceiling(m / 200) * 5 + Math.Floor((st2 - st1) / 80) * 5
Return price
End Function
Function totsec(ByVal st As String) As Integer '總秒數
totsec = Val(Mid(st, 1, 2)) * 3600 + Val(Mid(st, 4, 2)) * 60 + Val(Mid(st, 7, 2))
End Function
P16
Const MaxM As Integer = 10 '邊長最大為 10
Dim dx() As Integer = {-2, -1, +1, +2, +2, +1, -1, -2}
Dim dy() As Integer = {-1, -2, -2, -1, +1, +2, +2, +1}
Function fnxx(ByVal s As String) As String
fnxx = ""
Dim dat() = s.Split(",")
Dim m As Integer = dat(0)
Dim sx As Integer = Asc(Mid(dat(1), 1, 1)) - Asc("A") '起點x
Dim sy As Integer = Val(Mid(dat(1), 2, 1)) - 1 '起點y
Dim ex As Integer = Asc(Mid(dat(2), 1, 1)) - Asc("A") '終點x
Dim ey As Integer = Val(Mid(dat(2), 2, 1)) - 1 '終點y
' 座標改為數字:左上(0,0)開始
Dim noway As Boolean = False '沒路
Dim a(MaxM, MaxM) As Boolean '是否走過
Dim nx, ny, d As Integer '下一步 , 方向序 d
Dim cnt As Integer = 0
Do Until sx = ex And sy = ey Or noway
a(sx, sy) = True
' fnxx &= cnt & ":" & sx & "," & sy & " "
' Print(3, cnt & ":" & sx & "," & sy & " ")
For d = 0 To 7
nx = sx + dx(d) : ny = sy + dy(d)
If ((nx < 0 Or nx >= m Or ny < 0 Or ny >= m) OrElse a(nx, ny)) Then Continue For
' 可以走 計數加1
cnt += 1
sx = nx : sy = ny
Exit For ' 進到下一格
Next
If (d = 8) Then noway = True ' 八個方向都找不到可走的格子
Loop
If noway Then fnxx = -1 Else fnxx = cnt
End Function