| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1419 人关注过本帖
标题:编写“宫格数”程序遇到的非代码难点!
取消只看楼主 加入收藏
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
结帖率:37.14%
收藏
已结贴  问题点数:10 回复次数:6 
编写“宫格数”程序遇到的非代码难点!
本人想利用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
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
收藏
得分:0 
回复 2楼 风吹过b
能否详细解释一下你所讲述关于该程序代码的思路?

飞点燃费盐--历史上至有型的计算机病毒(能感染你是我的荣幸,也是你的光荣!)
2011-08-30 16:29
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
shi781203
Rank: 2
来 自:广东省
等 级:论坛游民
帖 子:112
专家分:50
注 册:2011-3-18
收藏
得分:0 
以下是引用风吹过b在2011-9-6 11:30:30的发言:

我按你的 程序重新修改了 ,跑了一遍,没得到结果,
程序在 10秒之内就跑完了。

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

为何连九宫格都这么难搞啊?如果想编写100宫格及100以上宫格,那还得必须把九宫格写好才能有所突破啊!

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

https://bbs.bccn.net/viewthread.php?tid=293824

你参考一下这个贴子吧。

我参考并高试了,但结果还是令人感到遗憾!

飞点燃费盐--历史上至有型的计算机病毒(能感染你是我的荣幸,也是你的光荣!)
2011-09-08 16:26
快速回复:编写“宫格数”程序遇到的非代码难点!
数据加载中...
 
   



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

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