问答题

找出100以内所有可以拆成3个连续自然数之和的数(如:6=1+2+3),请将程序补充完整。
Option Explicit
Private Sub Command1_Click()
    Dim I As Integer, N As Integer
    Dim Js As Integer, S As String
    For I = 1 To 100
        If Fun(I, N, Js) Then
            S = Str(I) & " = " & N
Do         
                N = N + 1
                S = S &" + " & N
            Loop
            List1.AddItem S
        End If
    Next I
End Sub
Private Function Fun(L As Integer, M As Integer, Js As Integer) As Boolean
    Dim I As Integer, Sum As Integer, K As Integer
    For I = 1 To L
       
        K = 0
       
        Do While Sum < LAnd K < 3
            K = K + 1
            Js = Js + 1
            Sum = Sum + Js
        Loop
        If Sum = L And K = 3 Then              
            M = I
            Fun = True
            Exit For
        End If
    Next I
End Function

答案: (1)
While N < Js ...
微信扫码免费搜题