您的位置:程序门 -> vb -> 基础类



vb循环比较的问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb循环比较的问题
发表于:2008-01-17 18:36:11 楼主
我想算四个数的运算结果,然后和一个小数比较,合适的话就输出。。
我用的是下面的方法
dim   a   as   integer,b   as   integer,c   as   integer,d   as   integer
dim   x   as   double
  for   a=20   to   150
      for   b=20   to   150  
          for   c=20   to   150
              for   d=20   to   150
              if   (a*b)/(c*d) <=x   then  
                text1.text=a
                text2.text=b
                text3.text=c
                text4.text=d
            end   if
      next   d
      next   c
      next   b
      next   a  
但是当x很小的时候,运算太慢了,几乎死机了。。
各位大哥看看,小弟怎么才能求出a,b,c,d   啊!
发表于:2008-01-17 18:49:311楼 得分:0
dim       a       as       integer,b       as       integer,c       as       integer,d       as       integer  
dim       x       as       double  
    for       a=20       to       150  
            for       b=20       to       150      
                    for       c=150   to   20   step   -1    
                            for       d=150       to     20   step   -1
                            if       (a*b)/(c*d)   <=x       then      
                                text1.text=a  
                                text2.text=b  
                                text3.text=c  
                                text4.text=d  
                        end       if  
            next       d  
            next       c  
            next       b  
            next       a
发表于:2008-01-17 19:03:232楼 得分:0
大哥为什么这样就可以啊,小弟在网吧不能实验,能说明一下吗。谢谢
发表于:2008-01-17 19:15:353楼 得分:0
a   与   b,   c   与   d   是完全对等的,不必从头到尾计算一遍。

dim               a               as               integer,b               as               integer,c               as               integer,d               as               integer      
dim               x               as               double

dim   m   as   long,   n   as   long,   tmp   as   long
for   m   =   20   ^2   to   150   ^   2
      n   =   m   \   x
      if   n   >   150   ^   2   then   exit   for
      if   n   >   20   ^   2   then
            debug.print   "following   (a   *   b)   /   (c   *   d)   <   "   &   x
            tmp   =   m   ^   0.5
            for   a   =   tmp   to   20   step   -1
                  b   =   m   \   a
                  if   b   >   150   exit   for
                  if   b   > =   20   then   debug.print   "a   =   "   &   a,   "b   =   "   &   b
            next   a
            debug.print   ""
            tmp   =   n   ^   0.5
            for   c   =   tmp   to   20   step   -1
                  d   =   n   \   c
                  if   d   >   150   exit   for
                  if   d   > =   20   then   debug.print   "c   =   "   &   c,   "d   =   "   &   d
            next   a  
            debug.print   ""
      end   if
next   m            
发表于:2008-01-17 19:19:574楼 得分:0
循环次数的问题。
如果x很小,你的c,d都从最大的数开始递减的话,就可以少算很多次

我是这样理解的
发表于:2008-01-17 19:35:125楼 得分:0
没看明白这个题意是什么意思,
楼主还是先把原本的题目说出来,或许更容易理解。
发表于:2008-01-17 19:38:246楼 得分:0
就是有四个数:a,b,c,d
要求   (a*b)/(c*d)   <=x
x为任意一个小数
求出满足要求的所有a,b,c,d
楼上大哥明白了吗
发表于:2008-01-17 19:40:417楼 得分:0
其中a,b,c,d的取值范围都是20到150
发表于:2008-01-17 19:47:248楼 得分:0
哪个大哥来帮帮忙啊
发表于:2008-01-17 19:53:539楼 得分:0
我解释一下我最后一段代码的思路:

1   a   *   b   的乘积的定义域是   20   的平方至   150   的平方,做这个循环。也就是先取得积   m   。
2   n   =   m   \   x   就是   c   *   d   的积。如果满足   20   ^2   <=   n   <=   150   则,这组   m,   n   就是符合条件的积。
3   把   m   和   n   分解成   2   *   b,   c   *   d。由于   a   和   b,   以及   c   和   d   是可互换的,解出一组足矣。所以最大循环至   m   平方根和   n   的平方根。    
发表于:2008-01-17 20:09:4010楼 得分:0
dim   a   as   integer,b   as   integer,c   as   integer
dim   x   as   double
    for   a=20   to   150      
            for   b=20   to   15-a
                for   c=20       to       150    
                        for   d=20   to   150-a   -b
                                text1.text=a      
                                text2.text=b      
                                text3.text=c      
                                text4.text=150-a-b-c
                next   c      
            next   b      
        next   a
发表于:2008-01-17 20:15:1111楼 得分:0
发错了
发表于:2008-01-21 17:39:5412楼 得分:0
顶,哪个大哥帮帮忙啊
发表于:2008-01-21 18:00:2413楼 得分:0
但是看你得程序只能得到一组解呀。
text1.text=a      
text2.text=b      
text3.text=c      
text4.text=d  
即使前面满足了,也会被下一组满足的数据给冲掉
发表于:2008-01-22 17:30:3214楼 得分:0
可以用
print   a,b,c,d
来输出啊,关键是如何采用一个更合理的算法来取代for     next   四重循环啊


快速检索

最新资讯
热门点击