2012年11月5日 星期一

判斷是否為11 的倍數

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

輸出範例:
0
1

4 則留言:

  1. Dim mycount, equal1, equal2 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, mycount
    For i = 1 To mycount
    Input #1, num
    For ii = 1 To Len(num)
    If Val(Mid(num, ii, 1)) Mod 2 = 1 Then equal1 = equal1 + Val(Mid(num, ii, 1))
    If Val(Mid(num, ii, 1)) Mod 2 = 0 Then equal2 = equal2 + Val(Mid(num, ii, 1))
    Next
    If equal1 = equal2 Then Print #2, 1
    If Not equal1 = equal2 Then Print #2, 0
    equal1 = 0
    equal2 = 0
    Next
    Close #2
    Close #1
    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
    sum1 = 0: sum2 = 0
    Input #1, x
    For m = 1 To Len(x)
    If m Mod 2 = 0 Then sum1 = sum1 + Val(Mid(x, m, 1))
    If m Mod 2 = 1 Then sum2 = sum2 + Val(Mid(x, m, 1))
    Next
    If (sum1 - sum2) Mod 11 = 0 Then Print #2, "1" Else Print #2, "0"
    Next
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Bob好:)
    你的程式少一個判斷喔
    查查看11的倍數有什麼特點

    回覆刪除
  4. 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 j = 1 To n
    Line Input #1, w
    lon = Len(w)
    For i = 1 To lon Step 2
    x = Val(Mid(w, i, 1)) + x
    y = Val(Mid(w, i + 1, 1)) + y
    Next
    ans = (x - y) Mod 11
    If ans = 0 Then Print #2, "1" Else Print #2, "0"
    x = 0
    y = 0
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除