2012年2月13日 星期一

判斷是否為11 的倍數

給一個正整數n,請寫一個程式,判斷n 是否為11 的倍數?
輸入說明:
第一行的數字,表示有幾組測試資料,第二行開始即為第一筆測試資料。每筆測試資料
為一個正整數,數字的位數,最高有可能到1000 位。
輸出說明:
對每一筆測試資料,輸出是否為 11 的倍數。是的話請輸出1,反之則輸出0。
輸入範例:
2
24841983960
121

輸出範例:
0
1

4 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Dim a, ans As Integer
    Input #1, n
    For i = 1 To n
    Input #1, x
    a = Len(x)
    b = 0
    c = 0
    ans = 0
    For y = 1 To a Step 2
    b = Val(Mid(x, y, 1))
    ans = ans + b
    Next
    For j = 2 To a Step 2
    c = Val(Mid(x, j, 1))
    ans = ans - c
    Next
    If ans Mod 11 = 0 Then
    Print #2, "1"
    Else
    Print #2, "0"
    End If
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    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
    c = 0
    ans = 0
    Input #1, X
    a = Len(X)
    For k = 1 To a Step 2
    c = Val(Mid(X, k, 1))
    ans = ans + c
    Next k

    For j = 2 To a Step 2
    c = Val(Mid(X, j, 1))
    ans = ans - c
    Next j
    If ans Mod 11 = 0 Then
    Print #2, "1"
    Else
    Print #2, "0"
    End If
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    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, n
    a = Len(n)
    b = 0
    c = 0
    ans = 0

    For j = 1 To a Step 2
    b = Val(Mid(n, j, 1))
    ans = ans + b
    Next j

    For k = 2 To a Step 2
    c = Val(Mid(n, k, 1))
    ans = ans - c
    Next k

    If ans Mod 11 = 0 Then
    Print #2, "1"
    Else
    Print #2, "0"
    End If
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Dim b() As String
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, s
    For j = 1 To s
    Input #1, r
    ReDim b(Len(r))
    d = 0
    e = 0
    For i = 1 To Len(r)
    b(i) = Val(Mid(r, i, 1))
    If i Mod 2 = 0 Then
    d = b(i) + d
    Else
    e = b(i) + e
    End If
    Next i
    If (d - e) Mod 11 = 0 Or d - e = 0 Then
    Print #2, 1
    Else
    Print #2, 0
    End If
    Next j
    Close #2
    Close #1
    End
    End Sub

    回覆刪除