輸入範例:
【in1.txt】
3
0,2,6
13,10,11
27,36,23
【in2.txt】
3
26,42,8
11,25,30
39,21,22
|
輸出範例:
【out.txt】
21
21
Bom
14
Bom
20
|
輸出說明:
索引:點數 …
0:1 1:3 2:7 21 11+
3+ 7 = 21
0:1 1:10 2:10 21 1 +10+10 = 21
0:2 1:10 2:10 Bom 2 + 10+10 = 22 Bom
0:1 1:4 2:9 14 1 + 4 + 9 = 14
0:10 1:10 2:5 Bom 10+10+ 5 = 25 Bom
0:1 1:9 2:10 20 1 + 9+10 = 20
|
程式碼:
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", OpenMode.Input)
FileOpen(2, "in2.txt", OpenMode.Input)
FileOpen(3, "out.txt", OpenMode.Output)
For fn = 1 To 2
If fn = 2 Then PrintLine(3)
Dim n As Short = LineInput(fn)
For k = 1 To n
Dim s = LineInput(fn)
PrintLine(3, fxx(s))
Next
Next
End
End Sub
Function fxx(ByVal s As String) As String
fxx = ""
Dim dat() = s.Split(",")
Dim sum As Integer = 0
Dim ace As Boolean = False
For i = 0 To 2
Dim pt As Integer = dat(i) Mod 13 + 1
If pt > 10 Then pt = 10
' Print(3, i & ":" & pt & " ")
sum += pt
If pt = 1 Then ace = True
Next
If ace And sum <= 11 Then sum += 10
If sum > 21 Then Return "Bom" Else Return sum
End Function
End Class
1227-5_大數加乘(參考解)
大數相加或大數相乘:兩個正整數a,b中間一個+或*,算出其結果,{若0}<=a,b<1050
輸入範例:
【in1.txt】
3
12345+98765432123456789
56*89
7995979239752398752987925925225252*0
【in2.txt】
3
567+433
2147483648*7487812485248974848
0+0
|
輸出範例:
【out.txt】
98765432123469134
4984
0
1000
16079954871362414694843285504
0
|
輸出說明:
乘0印出一個0
0+0也是0
|
程式碼:
Public Class Form1
Const MaxN As Integer = 50
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Hide()
FileOpen(1, "in1.txt", OpenMode.Input)
FileOpen(2, "in2.txt", OpenMode.Input)
FileOpen(3, "out.txt", OpenMode.Output)
For fn = 1 To 2
If fn = 2 Then PrintLine(3)
Dim n As Short = LineInput(fn)
For k = 1 To n
Dim s = LineInput(fn)
PrintLine(3, fxx(s))
Next
Next
End
End Sub
Sub s2n(ByVal xs As String, ByRef x() As Integer, ByRef xn As Integer)
'將數字串 倒轉為 陣列
xn = xs.Length
Dim i, j As Integer
For i = xn To 1 Step -1
x(j) = Mid(xs, i, 1)
j += 1
Next
cut0(x, xn)
End Sub
Sub cut0(ByRef x() As Integer, ByRef xn As Integer)
Do While xn > 0 AndAlso x(xn - 1) = 0
xn -= 1
Loop
End Sub
Function fxx(ByVal s As String) As String
fxx = ""
Dim a(MaxN), b(MaxN), c(MaxN + MaxN) As Integer '會清為零,若只宣告一次需記得清
Dim an, bn, cn As Integer
Dim p As Integer = InStr(s, "+")
If p > 0 Then
s2n(Mid(s, 1, p - 1), a, an)
s2n(Mid(s, p + 1), b, bn)
big_add(a, an, b, bn, c, cn)
Else
p = InStr(s, "*")
s2n(Mid(s, 1, p - 1), a, an)
s2n(Mid(s, p + 1), b, bn)
big_mul(a, an, b, bn, c, cn)
End If
fxx = big_print(c, cn)
End Function
Sub big_add(ByVal x() As Integer, ByVal xn As Integer, ByVal y() As Integer, ByVal yn As Integer, ByRef z() As Integer, ByRef zn As Integer)
' PrintLine(3, big_print(x, xn) & " add " & big_print(y, yn))
zn = Math.Max(xn, yn)
For i = 0 To zn - 1
z(i) = x(i) + y(i)
Next
For i = 0 To zn - 1
If z(i) >= 10 Then
z(i + 1) += 1
z(i) -= 10
End If
Next
If z(zn) <> 0 Then zn += 1
End Sub
Sub big_mul(ByVal x() As Integer, ByVal xn As Integer, ByVal y() As Integer, ByVal yn As Integer, ByRef z() As Integer, ByRef zn As Integer)
' PrintLine(3, big_print(x, xn) & " mul " & big_print(y, yn))
zn = xn + yn
For i = 0 To xn - 1
For j = 0 To yn - 1
z(i + j) += x(i) * y(j)
Next
Next
For i = 0 To zn - 1
If z(i) >= 10 Then
z(i + 1) += z(i) \ 10
z(i) = z(i) Mod 10
End If
Next
cut0(z, zn)
End Sub
Function big_print(ByVal x() As Integer, ByVal xn As Integer) As String
Dim bp As String = ""
If xn = 0 Then Return "0"
For i = xn - 1 To 0 Step -1
bp &= x(i)
Next
Return bp
End Function
End Class
0 意見:
張貼留言