2010年2月9日 星期二

2010/02/09 聯集

讀入兩個正整數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

12 則留言:

  1. 20 到20 之間 位捨麼 有7個@@

    回覆刪除
  2. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #1
    Dim q(3) As Integer
    q(1) = 2: q(2) = 3: q(3) = 5
    Do Until EOF(1)
    Input #1, a, b
    If a = 0 And b = 0 Then Exit Do
    num = 0
    For i = a To b
    m = 1: Max = 0
    If i Mod q(m) <> 0 Then Max = Max + 1
    m = m + 1
    If i Mod q(m) <> 0 Then Max = Max + 1
    m = m + 1
    If i Mod q(m) <> 0 Then Max = Max + 1
    If Max > 1 Then num = num + 1
    Next i
    Print #2, num
    Loop
    Close #2
    Close #1
    End Sub
    輸入
    1 100
    20 20
    0 0
    輸出
    74
    0

    回覆刪除
  3. Private Sub Form_Load()
    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 Then Exit Do
    ans = 0
    For i = x To y
    If i Mod 2 = 0 Then ans = ans + 1
    If i Mod 3 = 0 Then ans = ans + 1
    If i Mod 5 = 0 Then ans = ans + 1
    If i Mod 6 = 0 Then ans = ans - 1
    If i Mod 10 = 0 Then ans = ans - 1
    If i Mod 15 = 0 Then ans = ans - 1
    If i Mod 30 = 0 Then ans = ans - 2
    Next i
    Print #2, ans
    Loop
    Close #1
    Close #2
    End Sub

    回覆刪除
  4. Y揚好,
    1、開啟的兩個檔案號碼,還是習慣成#1,#2就好了。
    2、20 20 本身也算,所以會有1個吧。
    3、這題可以更簡單的吧,你中間用的很複雜,但也是算對的啦。
    (只需要一個if的,你用了3個)

    阿瑋好,
    1、程式該是正確,但是,你用的比Y的三個if還多,你用了7個,應該是只要1個的吧。
    再想想。

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

    回覆刪除
  6. 高仔好,
    你的程式基本上是對了,而且也達到了只用一個 if了,只是,
    If x = 0 And y = 0 Then Exit Sub
    這行用 exit do就可以了吧,用exit sub,你乾脆用end算了。
    還有,你用exit sub的話,那麼close #1 /close #2不會被執行到吧?
    有機會出現檔案不完整的錯誤哦。

    回覆刪除
  7. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, a, b
    Max = 0
    If a = 0 And b = 0 Then Exit Do
    For i = a To b
    m = 1
    If i Mod 2 <> 0 Or i Mod 3 <> 0 Or i Mod 5 <> 0 Then Max = Max + 1
    Next i
    Print #2, Max
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除
  8. Y揚好,
    你的程式是錯的吧?
    m=1
    If i Mod 2 <> 0 Or i Mod 3 <> 0 Or i Mod 5 <> 0 Then Max = Max + 1
    這兩行是什麼意思呢?
    1 10
    10

    回覆刪除
  9. 用 = 就好了@@
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, a, b
    Max = 0
    If a = 0 And b = 0 Then Exit Do
    For i = a To b
    m = 1
    If i Mod 2 = 0 Or i Mod 3 = 0 Or i Mod 5 = 0 Then Max = Max + 1
    Next i
    Print Max
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除
  10. Y揚好,
    你還是沒說
    m=1
    要做啥?

    回覆刪除
  11. Dim x2(100) As Integer, x3(100) As Integer, x5(100) As Integer
    Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do
    For i = 0 To 100
    x2(i) = 0: x3(i) = 0: x5(i) = 0
    Next i
    c = 0: ans = 0
    Input #1, a, b
    For i = a To b
    q = a + c
    If q Mod 2 = 0 Then x2(c) = q: ans = ans + 1
    If q Mod 3 = 0 Then
    x3(c) = q
    If x3(c) <> 0 And x3(c) <> x2(c) Then ans = ans + 1
    End If
    If q Mod 5 = 0 Then
    x5(c) = q
    If x5(c) <> 0 And x5(c) <> x3(c) And x5(c) <> x2(c) Then ans = ans + 1
    End If
    c = c + 1
    Next i
    If a = b And a = 0 Then Exit Do
    Print #2, ans
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除
  12. Public 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 Sub
    ans = 0
    For i = x To y
    If i Mod 2 = 0 Then ans = ans + 1
    If i Mod 3 = 0 Then ans = ans + 1
    If i Mod 5 = 0 Then ans = ans + 1
    If i Mod 6 = 0 Then ans = ans - 1
    If i Mod 15 = 0 Then ans = ans - 1
    If i Mod 10 = 0 Then ans = ans - 1
    Next i
    Print #2, ans
    Loop
    Close #2
    Close #1
    End
    End Sub

    3分12

    回覆刪除