我想找到一个四位自然数,其前边两位与后边两位都是相同的数字(如1122),但是这个四位数同时也是一个自然数的平方。

解决方案 »

  1.   

    7744Function test() As Integer
        Dim a As Integer, b As Integer
        Dim i As Integer, j As Integer
        Dim x As Integer
        
        For i = 10 To 99
            If Left(CStr(i), 1) = Right(CStr(i), 1) Then
                a = i
            For j = 10 To 99
                If Left(CStr(j), 1) = Right(CStr(j), 1) Then
                    b = j
                    x = CInt(CStr(a) & CStr(b))
                    If Sqr(x) = Int(Sqr(x)) Then
                        test = x
                        Exit For
                    End If
                End If
            Next
            End If
        Next
        
    End Function
      

  2.   

    分析数字规律后,发现其实可以改进一下。
    因为两位相同的数字只能是11、22、33、44、55...,间隔是11,所以用11做for的步长,可减少100倍的循环量。
    Function test() As Integer
        Dim i As Integer, j As Integer
        Dim x As Integer
        
        For i = 11 To 99 Step 11
            For j = 11 To 99 Step 11
                x = CInt(CStr(i) & CStr(j))
                If Sqr(x) = Int(Sqr(x)) Then
                    test = x
                    Exit For
                End If
            Next
        Next
        
    End Function