云南玉龙雪山在哪里:输出由1,2,3,4四个数字组成的四位数字,并统计他们有多少种组合(注:四位数字可以相同,如1111)用VB解答

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 05:33:36
记得是用VB哦
用FOR语句回答

dim i as integer,j as integer,k as integer,l as integer
dim Counts as integer,OutNum as integer
counts=0
for i=1 to 4
for j=1 to 4
for k=1 to 4
for l=1 to 4
OutNum=val(i & j & k & l)'组成四位数,需要时可以保存
Print OutNum'为了验证组成的数字,输出在窗体上
counts=counts+1
next l
next k
next j
next i
print counts'组合数

可以相同的话就是 4的4次方,
这根本就不是组合的!求组合c(top,bottom)和排列p(top,bottom)函数如下:
<SCRIPT LANGUAGE="javascript">
function pl(top,bottom){//排列
var tmp;
if(top>bottom){
tmp=bottom;
bottom=top;
top=tmp;
}
if(top<=0) return 1
else return bottom * pl(top-1,bottom-1);
}
function zuhe(top,bottom){
var tmp,topV,bottomV;
topV=0;bottomV=0;
if(top>bottom){
tmp=bottom;bottom=top;top=tmp;
}
if(top<=0 || bottom<=0) return 1;
if(top>bottom-top) top = bottom-top;//C(3,4)=C(1,4)减少程序运行时间
topV = pl(top,top);
bottomV = pl(bottom,bottom);
return bottomV/topV;
}
alert(zuhe(3,6));
</SCRIPT>
使用VB自己改一下,function后不要{},最后用 end function,var改为 dim,把;去掉,把return换成 functionName(函数名赋值)
alert()改成 msgbox zuhe(3,6)

四层嵌套循环,每层都从1到4。