vb6 有关求和的问题

来源:百度知道 编辑:UC知道 时间:2024/09/27 05:47:47
给出10个随机自然数,如果其中几个数相加得到61,则把这几个数字并为一组并且print出来,直到找出所有可能的组.

例:
10个随机数为a0到a9
其中a0+a7=61
a1+a5+a9=61
a2=61
则结果为
a0,a7
a1,a5,a9
a2

这样的程序要怎么编?哪个高手能教教我?

很简单,需要的变量自行声明。
把10个数放到数组A(9)中,自己放。用下面的代码:
for i1=0 to 1:for i2=0 to 1:for i3=0 to 1:for i4=0 to 1:for i5=0 to 1:for i6=0 to 1:for i7=0 to 1:for i8=0 to 1:for i9=0 to 1:for i10=0 to 1
S=i1*A(0)+i2*A(1)+i3*A(2)+i4*A(3)+i5*A(4))+i6*A(5)+i7*A(6)+i8*A(7))+i9*A(8))+i10*A(9)
if s=61 then
B(0)=i1:b(1)=i2:B(2)=i3:B(3)=i4:B(4)=i5:B(5)=i6:b(6)=i7:B(7)=i8:B(8)=i9:B(9)=i10
for i=0 to 9
if b(i)<>0 then print a(i);
next i
print
end if
next:next:next:next:next:next:next:next:next:next

计算量不是很大!你就直接循环吧!
如果运算量大的话再考虑下算法!