2012年11月20日 星期二

聯集

讀入兩個正整數a以及b,請書出介於a及b之間(包含a、b)2、3、5倍數的聯集大小。
例如1及10之堅,是2倍數集合為{2、4、6、8、10};是3倍數為{3、6、9};是5倍數集合為{5、10},上述三個集合的聯集為{2、3、4、5、6、8、9、10},故聯集大小為8。
輸入規範:輸入檔案中可能包含了好幾列的測試資料,每一列有兩個整數(即a及b)。a=0、b=0代表輸入結束。
輸出規範:對每一列輸入,輸出聯集的大小(請參考輸出範例)。

輸入範例:
1  10
10  20
0  0
輸出範例:
8
7

4 則留言:

  1. Dim former, latter, ans As Integer
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, former, latter
    If former = latter And latter = 0 Then Exit Do
    ans = 0
    For i = former To latter
    If i Mod 2 = 0 Or i Mod 3 = 0 Or i Mod 5 = 0 Then ans = ans + 1
    Next
    Print #2, ans
    Loop
    Close #2
    Close #1
    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
    Do
    Input #1, x, y
    If x = 0 And y = 0 Then Exit Do
    t = 0
    For i = x To y
    If i Mod 2 = 0 Or i Mod 3 = 0 Or i Mod 5 = 0 Then t = t + 1
    Next
    Print #2, t
    Loop
    Close
    Close
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    Input #1, x
    Input #1, y
    If x = 0 And y = 0 Then Exit Do
    For i = Val(x) To Val(y)
    If i Mod 2 = 0 Or i Mod 3 = 0 Or i Mod 5 = 0 Then num = num + 1
    Next
    Print #2, num
    num = 0
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除