VB循环比较的问题

suncumt 2008-01-17 06: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 啊!
...全文
302 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
suncumt 2008-01-22
  • 打赏
  • 举报
回复
可以用
print a,b,c,d
来输出啊,关键是如何采用一个更合理的算法来取代for next 四重循环啊
熊孩子开学喽 2008-01-21
  • 打赏
  • 举报
回复
但是看你得程序只能得到一组解呀。
text1.text=a
text2.text=b
text3.text=c
text4.text=d
即使前面满足了,也会被下一组满足的数据给冲掉
suncumt 2008-01-21
  • 打赏
  • 举报
回复
顶,哪个大哥帮帮忙啊
z_wenqian 2008-01-17
  • 打赏
  • 举报
回复
发错了
z_wenqian 2008-01-17
  • 打赏
  • 举报
回复
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
of123 2008-01-17
  • 打赏
  • 举报
回复
我解释一下我最后一段代码的思路:

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 的平方根。
suncumt 2008-01-17
  • 打赏
  • 举报
回复
哪个大哥来帮帮忙啊
suncumt 2008-01-17
  • 打赏
  • 举报
回复
其中a,b,c,d的取值范围都是20到150
suncumt 2008-01-17
  • 打赏
  • 举报
回复
就是有四个数:a,b,c,d
要求 (a*b)/(c*d) <=x
X为任意一个小数
求出满足要求的所有a,b,c,d
楼上大哥明白了吗
熊孩子开学喽 2008-01-17
  • 打赏
  • 举报
回复
没看明白这个题意是什么意思,
楼主还是先把原本的题目说出来,或许更容易理解。
clear_zero 2008-01-17
  • 打赏
  • 举报
回复
循环次数的问题。
如果x很小,你的c,d都从最大的数开始递减的话,就可以少算很多次

我是这样理解的
of123 2008-01-17
  • 打赏
  • 举报
回复
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
suncumt 2008-01-17
  • 打赏
  • 举报
回复
大哥为什么这样就可以啊,小弟在网吧不能实验,能说明一下吗。谢谢
of123 2008-01-17
  • 打赏
  • 举报
回复
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

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧