0.1.2.3.4.5.6.7.8.9共10个数,如何取出其中6个数字的组合,应该是210种,如何把结果算出来? ( 积分: 100 )

Q

QXCOMM

Unregistered / Unconfirmed
GUEST, unregistred user!
0.1.2.3.4.5.6.7.8.9共10个数,如何取出其中6个数字的组合,应该是210种,如何把结果算出来?
 
0.1.2.3.4.5.6.7.8.9共10个数,如何取出其中6个数字的组合,应该是210种,如何把结果算出来?
 
没有人会吗
 
排列组合
[n * (n-1) * .. *(n-m+1)] / m!
//此处 n=10,m=6
 
我要是是具体的210个组合的结果,不是计算有多少种组合啊
 
xianguo,非常感谢,是我说的不清楚,你能再考虑考虑吗
 
最笨 的 方法,循环6次
 
能具体说说吗?如何循环6次?
 
//Delphi7;递归法
www.hitekersoft.com/download/Combination.exe
 
因为我的机子是没装Delphi
这是C语言的代码。
如果还有这样的题和我QQ:30600061联系。
#include<stdio.h>
main()
{
int a[10],i;
int j,t;
i=0;
t=0;
a[0]=0;

while(a[0]!=5)
{
if(a==i+5)
{
i--;
a=a+1;
}
else if(i!=5 )
{
i++;
a=a[i-1]+1;
}
else if(i==5)
{
t++;
for(j=0;j<6;j++)
printf(&quot;%3d&quot;,a[j]);
a++;
printf(&quot;/n&quot;);
/*
if ((t % 10)==0)
{
printf(&quot;/n&quot;);
getch();

}
*/
}
}
printf(&quot;t=%d&quot;,t);
getch();
}
 
xianguo解答的不错,学习。。。
 
/*
本程序主要用了回溯算法
其中t是用来统计所有组合数;
a数组是用来临时存放组合;

*/


#define MAX 50
#include<stdio.h>
main()
{
int a[MAX],i;
int j,t;
int m,n;
i=0;
t=0;
a[0]=0;
m=10;
n=6;

while(a[0]!=m-n+1)
{
if(a==i+m-n+1) /* 因为是从0开始所以要+1 如果从1开始就不用+1 */
{
i--;
a=a+1;
}
else if(i!=n-1) /* 因为是从0开始所以要-1 如果从1开始就不用-1 */
{
i++;
a=a[i-1]+1;
}
else if(i==n-1) /* 因为是从0开始所以要-1 如果从1开始就不用-1 */
{
t++;
for(j=0;j<n;j++)
printf(&quot;%3d&quot;,a[j]);
a++;
printf(&quot;/n&quot;);
/* 下面代码是为了能看清所有组合而打上就的
if ((t % 10)==0)
{
printf(&quot;/n&quot;);
getch();

}
*/
}
}
printf(&quot;t=%d&quot;,t);
getch();
}
 
var
i ,j,k,l,m,n,o : integer;
t : string;
begin
for i := 0 to 9 do
begin
for j := 0 to 9 do
begin
for k := 0 to 9 do
begin
for l := 0 to 9 do
begin
for m:= 0 to 9 do
begin
for n :=0 to 9 do
begin
for o :=0 to 9 do
begin
t := inttostr(i) +inttostr(j) + inttostr(k) +inttostr(l) + inttostr(m) + inttostr(n) + inttostr(o);
memo1.Lines.Add(t);
end;
end;
end;
end;
end;
end;

end;
 
memo1的内容显示的就是你需要的内容吧?
 
按henai的方法,那答案不止210种啊。
 
在henai的基础上改了一下,henai你的那个运行出来有10万多个吧好像[:(]
Delphi7+Win2K调试通过
var
i ,j,k,l,m,n,count: Integer;
t : String;
begin
count:=0;
for i := 1 to 10 do
begin
for j := i to 10 do
begin
if j=i then
continue;
for k := j to 10 do
begin
if (k=j) or (k=i) then
continue;
for l := k to 10 do
begin
if (l=k) or (l=j) or (l=i) then
continue;
for m:= l to 10 do
begin
if (m=l) or (m=k) or (m=j) or (m=i) then
continue;
for n :=m to 10 do
begin
if (n=m) or (n=l) or (n=k) or (n=j) or (n=i) then
continue;
t := inttostr(i) +','+inttostr(j) + ','+inttostr(k)+','
+inttostr(l) + ','+inttostr(m) + ','+inttostr(n);
memo1.Lines.Add(t);
count:=count+1;
end;
end;
end;
end;
end;
end;
ShowMessage(IntToStr(count));
end;
请楼主过目!!嘿嘿。
 
楼上的朋友写的代码应该就是楼主要的吧,我自己也是这么想的,
刚想留言,被楼上的朋友抢先了。
 
刚才的代码还有一点问题。修改如下:
代码:
var
 i ,j,k,l,m,n,count: Integer;
 t : String;
begin
  count:=0;
  for i := 1 to 10 do
  begin
     for j := i+1 to 10 do
      begin
        for k := j+1 to 10 do
        begin
          for l := k+1 to 10 do
          begin
            for m:= l+1 to 10 do
            begin
              for n :=m+1 to 10 do
              begin
                t := inttostr(i) +','+inttostr(j) + ','+inttostr(k) +','+inttostr(l) + ','+inttostr(m) + ','+inttostr(n);
                memo1.Lines.Add(t);
                count:=count+1;
              end;
            end;
          end;
        end;
      end;
  end;
  ShowMessage(IntToStr(count));
end;
这个算法是正确的,我中午回去证明了一下,如果对其证明有兴趣的话,可以联系我。
swordwindplaust@126.com
 
楼主怎么还不结贴啊。该结了吧。这么久了都没有反应。:(
 
你们的代不行啊,是6个组合
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
顶部