胡孝啟與女友方月花學了一個計算兩人緣份的方法,就是將兩人姓名中每個字的筆劃穿插排成一排,男生的名字先放,也就是若男生名字以男1 男2 男3三個字表示,女生名字以女1 女2 女3表示,則放筆劃的順序為男1女1男2女2男3女3(如下圖最上面一層之數字,胡孝啟三個字比劃分別為9,7,11,方月華為4,4,8,則先將這六個數排成 9474118)。然後兩個兩個相鄰數字相加後取個位數,形成第二層,這樣一層一層累加到最後剩下兩位數字即停,此兩位數就是他倆的緣份評分。例如:下圖第二層為9+4=13取個位數3, 4+7=11取1, 7+4=11取1, 4+1=5, 1+1=2, 1+8=9,算完後形成311529。以此類推,他倆的緣份計算方法就如下圖,最後得到52分。
請你設計一程式,讓佳偶只要輸入姓名筆劃之排列(如9474118),電腦就能自動計算出他倆的緣份分數。
輸入檔格式 (c:\match\input.txt)
檔案中第一行為測試對數,第二行為第一對男女比劃之排列,第三行為第二對男女比劃之排列,以此類推。
輸出檔格式 (c:\match\output.txt)
請由檔案輸出各對配對分數,每對以一行顯示。
輸入範例:
3
9474118
4112010139
101411579
輸出範例:
52
96
04
Dim N, A, B, X, Y As String
回覆刪除Private Sub Form_Load()
Open App.Path & "/in.txt" For Input As #1
Open App.Path & "/out.txt" For Output As #2
Input #1, N
For i = 1 To N
Input #1, X
RE:
For j = 1 To Len(X) - 1
A = Mid(X, j, 1)
B = Mid(X, j + 1, 1)
Y = Y & (Val(A) + Val(B)) Mod 10
Next j
X = Y
Y = ""
If Len(X) > 2 Then GoTo RE
Print #2, X
Next i
Close #2
Close #1
End Sub
高仔好,
回覆刪除這題OK。
你的程式中,有個goto 的地方,這部分如果要改成do loop,會嗎?
可以嘗試看看,有goto的程式,稱作比較不「結構化」而已,其實是沒有關係的,但是,反正是學習,試試不用goto也是練習。
Private Sub Form_Load()
回覆刪除Dim num As String
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, x
For i = 1 To x
Input #1, num
nw = num
Do Until Len(nw) = 2
nw = ""
Do Until Len(num) = 1
s = Right(num, 2)
s1 = Right(s, 1)
s2 = Left(s, 1)
st = Val(s1) + Val(s2)
If Len(st) = 1 Then
nw = st & nw
Else
nw = Right(st, 1) & nw
End If
num = Left(num, Len(num) - 1)
Loop
num = nw
Loop
Print #2, nw
Next i
Close #1
Close #2
End Sub
ㄚ揚好,
回覆刪除程式ok。
If Len(st) = 1 Then
nw = st & nw
Else
nw = Right(st, 1) & nw
End If
這5行,改成一行就好了吧。
nw = Right(st, 1) & nw
Private Sub Form_Load()
回覆刪除Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2 '
Input #1, n
For a = 1 To n
Line Input #1, x
Do
StrX = ""
For i = 1 To Len(x) - 1
L = Mid(x, i, 1)
R = Mid(x, i + 1, 1)
k = (Val(L) + Val(R))
StrX = StrX & k Mod 10
Next i
x = StrX
Loop Until Len(x) = 2
Print #2, x
Next a
Close #2
Close #1
End Sub
Dim a(12) As Integer, b(12) As Integer
回覆刪除Private Sub Form_Load()
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, qqq
For iii = 1 To qqq
Line Input #1, StrQ
x = Len(StrQ)
For i = 1 To x
a(i) = Val(Mid(StrQ, i, 1))
Next i
c = 1
Do Until x = 0
If c = 1 Then
For i = 1 To x - 1
b(i) = (a(i) + a(i + 1)) Mod 10
Next i
c = c - 1
x = x - 1
ans = b(1) * 10 + b(2)
Else
For i = 1 To x - 1
a(i) = (b(i) + b(i + 1)) Mod 10
Next i
c = c + 1
x = x - 1
ans = a(1) * 10 + a(2)
End If
Loop
Print ans
Next iii
Close #1
Close #2
End Sub