| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1419 人关注过本帖
标题:编写“宫格数”程序遇到的非代码难点!
只看楼主 加入收藏
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
结帖率:37.14%
收藏
已结贴  问题点数:10 回复次数:13 
编写“宫格数”程序遇到的非代码难点!
本人想利用VB编写九宫格、十六宫格、二十五宫格、三十六宫格、四十九宫格、六十四宫格、八十一宫格等程序时遇到非代码难点无法解决请高手指点指点!
    其中九宫格数代码如下:
Dim x(1 To 9) As Integer    '代表九宫格的数值
Dim a(1 To 8) As Integer    '代表横向、坚向及斜向相加的数值
Dim TongGuo As Boolean        '当枚举出结果时就认程序停止

Private Sub Command1_Click()    '单击执行扭
TongGuo = False
        For s1 = 1 To 9    '分别枚举各宫格中的数值不论是否相同
            For s2 = 1 To 9
                For s3 = 1 To 9
                    For s4 = 1 To 9
                        For s5 = 1 To 9
                            For s6 = 1 To 9
                                For s7 = 1 To 9
                                    For s8 = 1 To 9
                                        For s9 = 1 To 9
                                            x(1) = s1
                                            x(2) = s2
                                            x(3) = s3
                                            x(4) = s4
                                            x(5) = s5
                                            x(6) = s6
                                            x(7) = s7
                                            x(8) = s8
                                            x(9) = s9
                                            Bs    '调用过程
                                            If TongGuo = True Then    '通过就显示结果到文本框1组中并跳出多层循环
                                                For i = 0 To 8
                                                    Text1(i).Text = x(i + 1)
                                                Next
                                                Exit For
                                            End If
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next
End Sub

Private Sub Bs()    '该过程用于排除九个格有相同数值的结果
    For i = 1 To 8
        For j = i + 1 To 9
            If x(i) <> x(j) Then
                TongGuo = True
            Else
                TongGuo = False
                Exit For
            End If
        Next
    Next
    If TongGuo = True Then    '把找到的结果临时显示到文本框2组中
        For i = 0 To 8
            Text2(i).Text = x(i + 1)
        Next
        a(1) = x(1) + x(2) + x(3)    '把找到的结果进行计算
        a(2) = x(4) + x(5) + x(6)
        a(3) = x(7) + x(8) + x(9)
        a(4) = x(1) + x(4) + x(7)
        a(5) = x(2) + x(5) + x(8)
        a(6) = x(3) + x(6) + x(9)
        a(7) = x(1) + x(5) + x(9)
        a(8) = x(3) + x(5) + x(7)
        For i = 1 To 7            '判断各横向、纵向及斜向的数值
            For j = i + 1 To 8
                If a(i) <> a(j) Then    '遇到不等就跳出
                    TongGuo = False
                    Exit For
                Else
                    TongGuo = True
                End If
            Next
        Next
    End If
    Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)     '临时显示结果
End Sub

程序运行时没有结果,最后需要单击关闭按扭强制退出程序,真认人扫兴,请高手们解答一下吧!
搜索更多相关主题的帖子: 九宫格 False 
2011-08-28 17:39
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:5 
你程序结构上有问题。
1、首先要产生所有的情况。你用的是 9层循环,这个倒也可以,但效率不高。如果你去计算 十六宫格,就需要 16 层循环。代码好难写。
   
2、找到的结果进行计算,也是写死的,灵活性差。
   这个公式里,存在四个变量,一个是最后值,一个是 三个 下标。完全可以把这些下标放到一直数组里,需要计算时,取下标,计算。可以使用循环来解决问题。

[ 本帖最后由 风吹过b 于 2011-8-29 12:25 编辑 ]
收到的鲜花
  • shi7812032011-09-06 10:53 送鲜花  1朵   附言:我很赞同

授人于鱼,不如授人于渔
早已停用QQ了
2011-08-29 12:12
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
你的代码的 效率 极低。我想的好久才想到。

9层循环,当第1层,第2层,相同时,
一共有 9^7 = 4782969 为无效循环。

所以,你循环时,需要先排除掉, 相同值的循环,才能进行下一步循环,否则你这个值,太大了。
收到的鲜花
  • shi7812032011-09-06 10:56 送鲜花  1朵   附言:后来我修改了程序,但还是得不到想要的结果 ...

授人于鱼,不如授人于渔
早已停用QQ了
2011-08-29 12:45
libralibra
Rank: 2
等 级:论坛游民
帖 子:8
专家分:19
注 册:2010-8-13
收藏
得分:5 
9层循环,
9**9 = 387,420,489
坑爹啊

接VB,MATLAB,python程序
QQ: 790404545
2011-08-30 05:40
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
收藏
得分:0 
回复 2楼 风吹过b
能否详细解释一下你所讲述关于该程序代码的思路?

飞点燃费盐--历史上至有型的计算机病毒(能感染你是我的荣幸,也是你的光荣!)
2011-08-30 16:29
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
一共是 9 个元素,每个元素取值为 1-9 。

每次变化时,使用递归进行调用。

最后一个值+1
〉9,置0
       前一个值+1
      〉9,置0 ........
<9 ,查找是否与前面的相同,相同,再继续加。
=0,查找第一个不相同的值给。

这种的递归下去。
直到第一个值〉9,递归失败,返回所有的值都计算完成了。
--------------------------------------------------------------------
9层循环的修改方法,我只改了一层。下面需要判断前面三个变量,依次增加

        For s1 = 1 To 9    '分别枚举各宫格中的数值不论是否相同
            For s2 = 1 To 9
                if s2〈〉s1 then
                 For s3 = 1 To 9
                    For s4 = 1 To 9
                        For s5 = 1 To 9
                            For s6 = 1 To 9
                                For s7 = 1 To 9
                                    For s8 = 1 To 9
                                        For s9 = 1 To 9
                                            x(1) = s1
                                            x(2) = s2
                                            x(3) = s3
                                            x(4) = s4
                                            x(5) = s5
                                            x(6) = s6
                                            x(7) = s7
                                            x(8) = s8
                                            x(9) = s9
                                            Bs    '调用过程
                                            If TongGuo = True Then    '通过就显示结果到文本框1组中并跳出多层循环
                                                For i = 0 To 8
                                                    Text1(i).Text = x(i + 1)
                                                Next
                                                Exit For
                                            End If
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
                end if
            Next
        Next

授人于鱼,不如授人于渔
早已停用QQ了
2011-08-30 21:01
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
收藏
得分:0 
回复 6楼 风吹过b
后来我更改了程序,但结果依然是令人感到遗憾……
为何会这样的,请高手指点指点吧!
Dim x(1 To 9) As Integer
Dim a(1 To 8) As Integer
Dim TongGuo As Boolean

Private Sub Command1_Click()
TongGuo = False

        For s1 = 1 To 9
            x(1) = s1
            For s2 = 1 To 9
                x(2) = s2
                If Bs(2) = False Then GoTo bs2    '如果比较出相同的值时,就跳出不必要的下层比较,而继续上一层的循环!(有不同值时才断续
                For s3 = 1 To 9                    '往下层进行比较)
                    x(3) = s3
                   If Bs(3) = False Then GoTo bs3
                    For s4 = 1 To 9
                        x(4) = s4
                      If Bs(4) = False Then GoTo bs4
                        For s5 = 1 To 9
                            x(5) = s5
                            If Bs(5) = False Then GoTo bs5
                            For s6 = 1 To 9
                                x(6) = s6
                               If Bs(6) = False Then GoTo bs6
                                For s7 = 1 To 9
                                    x(7) = s7
                                    If Bs(7) = False Then GoTo bs7
                                    For s8 = 1 To 9
                                        x(8) = s8
                                        If Bs(8) = False Then GoTo bs8
                                        For s9 = 1 To 9
                                            x(9) = s9
                                            If Bs(9) = False Then GoTo bs9
                                            
                                            bbs
                                            If TongGuo = True Then
                                                For i = 0 To 8
                                                    Text1(i).Text = x(i + 1)
                                                Next
                                                Exit Sub
                                            End If
bs9:

                                        Next
bs8:
                                    Next
bs7:
                                Next
bs6:
                            Next
bs5:
                        Next
bs4:
                    Next
bs3:
                Next
bs2:
            Next
        Next
End Sub

Function Bs(pd As Integer) As Boolean    '每一层循环都与之前所有层的数字进行比较
    For i = 1 To pd - 1
        For j = i + 1 To pd
            If x(i) <> x(j) Then
                TongGuo = True
            Else
                TongGuo = False
                Exit For
            End If
        Next
    Next
Bs = TongGuo
Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Function

Private Sub bbs()
    If TongGuo = True Then
        For i = 0 To 8
            Text2(i).Text = x(i + 1)
        Next
        a(1) = x(1) + x(2) + x(3)
        a(2) = x(4) + x(5) + x(6)
        a(3) = x(7) + x(8) + x(9)
        a(4) = x(1) + x(4) + x(7)
        a(5) = x(2) + x(5) + x(8)
        a(6) = x(3) + x(6) + x(9)
        a(7) = x(1) + x(5) + x(9)
        a(8) = x(3) + x(5) + x(7)
        For i = 1 To 7
            For j = i + 1 To 8
                If a(i) <> a(j) Then
                    TongGuo = False
                    Exit For
                Else
                    TongGuo = True
                End If
            Next
        Next
    End If
    Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Sub

[ 本帖最后由 shi781203 于 2011-9-6 10:58 编辑 ]

飞点燃费盐--历史上至有型的计算机病毒(能感染你是我的荣幸,也是你的光荣!)
2011-09-03 18:48
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
收藏
得分:0 
以下是引用shi781203在2011-8-28 17:39:07的发言:

本人想利用VB编写九宫格、十六宫格、二十五宫格、三十六宫格、四十九宫格、六十四宫格、八十一宫格等程序时遇到非代码难点无法解决请高手指点指点!
    其中九宫格数代码如下:
Dim x(1 To 9) As Integer    '代表九宫格的数值
Dim a(1 To 8) As Integer    '代表横向、坚向及斜向相加的数值
Dim TongGuo As Boolean        '当枚举出结果时就认程序停止

Private Sub Command1_Click()    '单击执行扭
TongGuo = False
        For s1 = 1 To 9    '分别枚举各宫格中的数值不论是否相同
            For s2 = 1 To 9
                For s3 = 1 To 9
                    For s4 = 1 To 9
                        For s5 = 1 To 9
                            For s6 = 1 To 9
                                For s7 = 1 To 9
                                    For s8 = 1 To 9
                                        For s9 = 1 To 9
                                            x(1) = s1
                                            x(2) = s2
                                            x(3) = s3
                                            x(4) = s4
                                            x(5) = s5
                                            x(6) = s6
                                            x(7) = s7
                                            x(8) = s8
                                            x(9) = s9
                                            Bs    '调用过程
                                            If TongGuo = True Then    '通过就显示结果到文本框1组中并跳出多层循环
                                                For i = 0 To 8
                                                    Text1(i).Text = x(i + 1)
                                                Next
                                                Exit For
                                            End If
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next
End Sub

Private Sub Bs()    '该过程用于排除九个格有相同数值的结果
    For i = 1 To 8
        For j = i + 1 To 9
            If x(i) <> x(j) Then
                TongGuo = True
            Else
                TongGuo = False
                Exit For
            End If
        Next
    Next
    If TongGuo = True Then    '把找到的结果临时显示到文本框2组中
        For i = 0 To 8
            Text2(i).Text = x(i + 1)
        Next
        a(1) = x(1) + x(2) + x(3)    '把找到的结果进行计算
        a(2) = x(4) + x(5) + x(6)
        a(3) = x(7) + x(8) + x(9)
        a(4) = x(1) + x(4) + x(7)
        a(5) = x(2) + x(5) + x(8)
        a(6) = x(3) + x(6) + x(9)
        a(7) = x(1) + x(5) + x(9)
        a(8) = x(3) + x(5) + x(7)
        For i = 1 To 7            '判断各横向、纵向及斜向的数值
            For j = i + 1 To 8
                If a(i) <> a(j) Then    '遇到不等就跳出
                    TongGuo = False
                    Exit For
                Else
                    TongGuo = True
                End If
            Next
        Next
    End If
    Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)     '临时显示结果
End Sub

程序运行时没有结果,最后需要单击关闭按扭强制退出程序,真认人扫兴,请高手们解答一下吧!

后来我更改了程序,但结果依然是令人感到遗憾……
为何会这样的,请高手指点指点吧!
Dim x(1 To 9) As Integer
Dim a(1 To 8) As Integer
Dim TongGuo As Boolean

Private Sub Command1_Click()
TongGuo = False

        For s1 = 1 To 9
            x(1) = s1
            For s2 = 1 To 9
                x(2) = s2
                If Bs(2) = False Then GoTo bs2    '如果比较出相同的值时,就跳出不必要的下层比较,而继续上一层的循环!(有不同值时才断续
                For s3 = 1 To 9                    '往下层进行比较)
                    x(3) = s3
                   If Bs(3) = False Then GoTo bs3
                    For s4 = 1 To 9
                        x(4) = s4
                      If Bs(4) = False Then GoTo bs4
                        For s5 = 1 To 9
                            x(5) = s5
                            If Bs(5) = False Then GoTo bs5
                            For s6 = 1 To 9
                                x(6) = s6
                               If Bs(6) = False Then GoTo bs6
                                For s7 = 1 To 9
                                    x(7) = s7
                                    If Bs(7) = False Then GoTo bs7
                                    For s8 = 1 To 9
                                        x(8) = s8
                                        If Bs(8) = False Then GoTo bs8
                                        For s9 = 1 To 9
                                            x(9) = s9
                                            If Bs(9) = False Then GoTo bs9
                                            
                                            bbs
                                            If TongGuo = True Then
                                                For i = 0 To 8
                                                    Text1(i).Text = x(i + 1)
                                                Next
                                                Exit Sub
                                            End If
bs9:

                                        Next
bs8:
                                    Next
bs7:
                                Next
bs6:
                            Next
bs5:
                        Next
bs4:
                    Next
bs3:
                Next
bs2:
            Next
        Next
End Sub

Function Bs(pd As Integer) As Boolean    '每一层循环都与之前所有层的数字进行比较
    For i = 1 To pd - 1
        For j = i + 1 To pd
            If x(i) <> x(j) Then
                TongGuo = True
            Else
                TongGuo = False
                Exit For
            End If
        Next
    Next
Bs = TongGuo
Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Function

Private Sub bbs()
    If TongGuo = True Then
        For i = 0 To 8
            Text2(i).Text = x(i + 1)
        Next
        a(1) = x(1) + x(2) + x(3)
        a(2) = x(4) + x(5) + x(6)
        a(3) = x(7) + x(8) + x(9)
        a(4) = x(1) + x(4) + x(7)
        a(5) = x(2) + x(5) + x(8)
        a(6) = x(3) + x(6) + x(9)
        a(7) = x(1) + x(5) + x(9)
        a(8) = x(3) + x(5) + x(7)
        For i = 1 To 7
            For j = i + 1 To 8
                If a(i) <> a(j) Then
                    TongGuo = False
                    Exit For
                Else
                    TongGuo = True
                End If
            Next
        Next
    End If
    Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Sub

[ 本帖最后由 shi781203 于 2011-9-6 10:32 编辑 ]

飞点燃费盐--历史上至有型的计算机病毒(能感染你是我的荣幸,也是你的光荣!)
2011-09-06 09:55
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
收藏
得分:0 
以下是引用风吹过b在2011-8-29 12:45:42的发言:

你的代码的 效率 极低。我想的好久才想到。

9层循环,当第1层,第2层,相同时,
一共有 9^7 = 4782969 为无效循环。

所以,你循环时,需要先排除掉, 相同值的循环,才能进行下一步循环,否则你这个值,太大了。


后来我按照你的意思修改了程序,但还是得不到想要的结果!
Dim x(1 To 9) As Integer
Dim a(1 To 8) As Integer
Dim TongGuo As Boolean

Private Sub Command1_Click()
TongGuo = False

        For s1 = 1 To 9
            x(1) = s1
            For s2 = 1 To 9
                x(2) = s2
                If Bs(2) = False Then GoTo bs2    '如果比较出相同的值时,就跳出不必要的下层比较,而继续上一层的循环!(有不同值时才断续
                For s3 = 1 To 9                    '往下层进行比较)
                    x(3) = s3
                   If Bs(3) = False Then GoTo bs3
                    For s4 = 1 To 9
                        x(4) = s4
                      If Bs(4) = False Then GoTo bs4
                        For s5 = 1 To 9
                            x(5) = s5
                            If Bs(5) = False Then GoTo bs5
                            For s6 = 1 To 9
                                x(6) = s6
                               If Bs(6) = False Then GoTo bs6
                                For s7 = 1 To 9
                                    x(7) = s7
                                    If Bs(7) = False Then GoTo bs7
                                    For s8 = 1 To 9
                                        x(8) = s8
                                        If Bs(8) = False Then GoTo bs8
                                        For s9 = 1 To 9
                                            x(9) = s9
                                            If Bs(9) = False Then GoTo bs9
                                            
                                            bbs
                                            If TongGuo = True Then
                                                For i = 0 To 8
                                                    Text1(i).Text = x(i + 1)
                                                Next
                                                Exit Sub
                                            End If
bs9:

                                        Next
bs8:
                                    Next
bs7:
                                Next
bs6:
                            Next
bs5:
                        Next
bs4:
                    Next
bs3:
                Next
bs2:
            Next
        Next
End Sub

Function Bs(pd As Integer) As Boolean    '每一层循环都与之前所有层的数字进行比较
    For i = 1 To pd - 1
        For j = i + 1 To pd
            If x(i) <> x(j) Then
                TongGuo = True
            Else
                TongGuo = False
                Exit For
            End If
        Next
    Next
Bs = TongGuo
Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Function

Private Sub bbs()
    If TongGuo = True Then
        For i = 0 To 8
            Text2(i).Text = x(i + 1)
        Next
        a(1) = x(1) + x(2) + x(3)
        a(2) = x(4) + x(5) + x(6)
        a(3) = x(7) + x(8) + x(9)
        a(4) = x(1) + x(4) + x(7)
        a(5) = x(2) + x(5) + x(8)
        a(6) = x(3) + x(6) + x(9)
        a(7) = x(1) + x(5) + x(9)
        a(8) = x(3) + x(5) + x(7)
        For i = 1 To 7
            For j = i + 1 To 8
                If a(i) <> a(j) Then
                    TongGuo = False
                    Exit For
                Else
                    TongGuo = True
                End If
            Next
        Next
    End If
    Text3.Text = a(1) & "," & a(2) & "," & a(3) & "," & a(4) & "," & a(5) & "," & a(6) & "," & a(7) & "," & a(8)
End Sub

飞点燃费盐--历史上至有型的计算机病毒(能感染你是我的荣幸,也是你的光荣!)
2011-09-06 10:38
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4947
专家分:30084
注 册:2008-10-15
收藏
得分:0 
我按你的 程序重新修改了 ,跑了一遍,没得到结果,
程序在 10秒之内就跑完了。

原因我也还在找,等有时间我重写一下吧。

授人于鱼,不如授人于渔
早已停用QQ了
2011-09-06 11:30
快速回复:编写“宫格数”程序遇到的非代码难点!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037768 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved