这道题目应该用什么样的算法啊?

解决方案 »

  1.   

    Private Sub combination()
        Dim a, b, c, i1, i2 As Integer
        For i1 = 1 To 30
            For i2 = 1 To 30
             If (i1 ^ 2 + i2 ^ 2) <= 30 Then
             a = i1
             b = i2
             c = i1 ^ 2 + i2 ^ 2
             Debug.Print a
             Debug.Print b
             Debug.Print c
             End If
            Next
        Next
    End Sub
      

  2.   

    穷举法
    Option ExplicitPrivate Sub Form_Click()
    Dim lngA As Long
    Dim lngB As Long
    Dim lngC As Long
    Const Min = 0
    Const Max = 30
        Me.Cls
        For lngA = Min To Max - 2
            For lngB = lngA + 1 To Max - 1
                For lngC = lngB + 1 To Max
                    If Not (lngA = lngB Or lngA = lngC Or lngB = lngC) Then
                        If (lngA ^ 2 + lngB ^ 2) = lngC ^ 2 Then
                            Print "   A = " & lngA; "    B = " & lngB; "    C = " & lngC
                        End If
                    End If
                Next lngC
            Next lngB
        Next lngA
    End Sub
      

  3.   

    上面的方法If Not (lngA = lngB Or lngA = lngC Or lngB = lngC) Then为多余,可以不要
      

  4.   

    由题目可得:
    a<>b 且 c>a and c>b可以通过一个类似于选择排序的方法来求得:
    Private Sub Form_Load()
     Me.AutoRedraw = True
    End SubPrivate Sub Command1_Click()
    Dim i As Integer, j As Integer, ct As Integer, tmp As Double
    ct = 0    '解个数
    For i = 1 To 29
      For j = i + 1 To 30
        If (i * i + j * j) > 900 Then Exit For '最大不可能大于30*30=900
        tmp = Sqr(i * i + j * j)
        If tmp = CInt(tmp) Then  '这里如果tmp能被开根号,则表示有解
          ct = ct + 1
          Print CStr(ct) & ": " & CStr(i) & "^2 + " & CStr(j) & "^2 = " & CStr(tmp) & "^2"
        End If
      Next
    Next
    Print "总共:" & CStr(ct) & "种方案"
    End Sub
    ---------------------------------------------------------------------------------
    以上代码通过测试,答案为:
    1: 3^2 + 4^2 = 5^2
    2: 5^2 + 12^2 = 13^2
    3: 6^2 + 8^2 = 10^2
    4: 7^2 + 24^2 = 25^2
    5: 8^2 + 15^2 = 17^2
    6: 9^2 + 12^2 = 15^2
    7: 10^2 + 24^2 = 26^2
    8: 12^2 + 16^2 = 20^2
    9: 15^2 + 20^2 = 25^2
    10: 18^2 + 24^2 = 30^2
    11: 20^2 + 21^2 = 29^2
    总共:11种方案
      

  5.   

    private sub zs()
      dim i,j as integer
      for i=1 to 30
         for j=1 to 30
            if (i*i+j*j)<=30 then
                 msgbox str(i),str(j),str(i*j)
            end if
         next i
      next i
      

  6.   

    Dim a, b, c, i1, i2 As Integer
    ....
    dim i,j as integer
    ...
    哎!,现在的VB学者,这种代码还随处可见,还有人是两个三角!