變化2:M5P05_2 書碼檢查(10碼)
給一組書碼,依下列檢查規則,若正確則輸出Yes,不正確則輸出No
(1) 書碼共10碼皆數字,非數字或長度不對則No
(2) 第1~9碼由左算第1碼為奇數位
所有奇數位*1、所有偶數位*3
所有奇數位*1、所有偶數位*3
(3) 第1~9位各乘上1或 3後加總後再加上第10位*1須能被10整除
in1.txt
2
1234567891
9876543215
|
in2.txt
2
2234567894
9976543211
|
out.txt
No
Yes
Yes
No
|
參考程式碼
Function M5P05_2(ByVal s As String) As String
M5P05_2 = "Yes"
If s.Length <> 10 Then Return
"No"
Dim sum As Integer = 0
For i = 1 To 10
If Mid(s, i, 1) < "0"
Or Mid(s, i, 1) > "9" Then Return "No"
Dim a As Integer = Val(Mid(s, i,
1))
Select Case i Mod 2
Case 1
sum += a * 1
Case 0
If i = 10 Then sum += a
Else sum += a * 3
End Select
Next
If sum Mod 10 <> 0 Then Return
"No"
End Function
變化3:M5P05_3 二信資處發票檢查
給一組二信資處合作社的發票,避免冒領獎金,依下列檢查規則,
若正確則輸出OK,不正確則輸出KO
若正確則輸出OK,不正確則輸出KO
(1) 發票號碼長度有可能7~9碼皆數字,非數字或長度不對則KO
(2) 第1,4,7碼*3、第2,5,8碼*2、第3,6,9碼*5,若無8,9碼當然不用乘
(3) 各碼分別乘上3,2,5後加總的和須能被7整除則OK否則KO
in1.txt
2
123456794
9876543
|
in2.txt
2
223456789
98765436
|
out.txt
OK
KO
KO
OK
|
參考程式碼
Function M5P05_3(ByVal s As String) As String
M5P05_3 = "OK"
If s.Length < 7 Or s.Length > 9
Then Return "KO"
Dim sum As Integer = 0
For i = 1 To s.Length
If Mid(s, i, 1) < "0"
Or Mid(s, i, 1) > "9" Then Return "KO"
Dim a As Integer = Val(Mid(s, i,
1))
Select Case i Mod 3
Case 1
sum += a * 3
Case 2
sum += a * 2
Case 0
sum += a * 5
End Select
Next
If sum Mod 7 <> 0 Then Return
"KO"
End Function
0 意見:
張貼留言