(1)類似題舉例1:判斷正整數0<N<1050是否可被
11 整除(因數字超過 Long型別)需使用字串處理
202914184810805067776
8239728901483491109728570834944
解題提示:奇數位的和 與 偶數位的和 是否為 11的倍數,是則輸出Y,不是則輸出N
參考程式碼如下:
Function M5P01(ByVal s As String) As String
Dim len As Integer = s.Length
Dim odd As Integer = 0 '奇數位和
Dim even As Integer = 0 '偶數位和
For i = 1 To len
If i Mod 2 = 0 Then
even += Val(Mid(s, i, 1))
Else
odd += Val(Mid(s, i, 1))
End If
Next
If Math.Abs(odd - even) Mod 11 = 0 Then
Return "Y"
Else
Return "N"
End If
End Function
-----------------------------------------
in1.txt
-----------------------------------------
2
202914184810805067776
8239728901483491109728570834944
-----------------------------------------
in2.txt
-----------------------------------------
3
285311670611
14300
99999999999999999999999999968877
-----------------------------------------
out.txt
-----------------------------------------
Y
N
Y
Y
N
0 意見:
張貼留言