小学奥数题 四位数相乘(VBA代码)

小学题:四位数乘以4等于另一四位数,1至8不重复【请找出各字母不重复代表1至8,且符合条件ABCD*4=EFGH的具体四位数?】

用VBA解决

Sub test()

Dim i, j, k  As Integer
Dim i1, i2, i3, i4, j1, j2, j3, j4



k = 0

For i = 1000 To 2499
    

    i1 = Int(i / 1000)
    i2 = Int((i - i1 * 1000) / 100)
    i3 = Int((i Mod 100) / 10)
    i4 = i - i1 * 1000 - i2 * 100 - i3 * 10


    If i1 <> i2 And i1 <> i3 And i1 <> i4 And i2 <> i3 And i2 <> i4 And i3 <> i4 Then
                          
            j = 4 * i
            j1 = Int(j / 1000)
            j2 = Int((j - j1 * 1000) / 100)
            j3 = Int((j Mod 100) / 10)
            j4 = j - j1 * 1000 - j2 * 100 - j3 * 10

            If j1 <> j2 And j1 <> j3 And j1 <> j4 And j2 <> j3 And j2 <> j4 And j3 <> j4 Then
            
               ' If i1 + i2 + i3 + i4 + j1 + j2 + j3 + j4 >= 28 And i1 + i2 + i3 + i4 + j1 + j2 + j3 + j4 <= 44 Then
                If j1 <> i1 And j1 <> i2 And j1 <> i3 And j1 <> i4 _
                And j2 <> i1 And j2 <> i2 And j2 <> i3 And j2 <> i4 _
                And j3 <> i1 And j3 <> i2 And j3 <> i3 And j3 <> i4 _
                And j4 <> i1 And j4 <> i2 And j4 <> i3 And j4 <> i4 Then
                
                
                       
                k = k + 1
                'MsgBox j
                Worksheets("VBA测试").Cells(k, "A").Value = j

                Worksheets("VBA测试").Cells(k, "B").Value = j1
                Worksheets("VBA测试").Cells(k, "C").Value = j2
                Worksheets("VBA测试").Cells(k, "D").Value = j3
                Worksheets("VBA测试").Cells(k, "E").Value = j4

                Worksheets("VBA测试").Cells(k, "F").Value = i
                
                Worksheets("VBA测试").Cells(k, "G").Value = i1
                Worksheets("VBA测试").Cells(k, "H").Value = i2
                Worksheets("VBA测试").Cells(k, "I").Value = i3
                Worksheets("VBA测试").Cells(k, "J").Value = i4
                
           
            
            End If
   
        End If
    
    End If
    
      
Next

End Sub 

答案:

1059;1089;1092;1209;1274;1368;1407;1408;1473;1735;1738;1745;1809;1826;1859;1863;1908;1956;1963;2039;2079;2091;2094;2109;2376;


你可能感兴趣的:(office,VBA,数学题目)