在选取红球时,另红球一编号比红球二编号小,即可得到所有的组合,具体语句为j=i+1,代码及截图如下,因为结果数据比较庞大,所以只截取开头和结尾
#include<iostream>
#include<iomanip>
using namespace std
void main()
{
int count=0
cout<<"组合种类 红球一 红球二 蓝球"<<endl
for(int i=1 i<=8 i++)
for(int j=i+1 j<=8 j++)
for(int k=1 k<=10 k++)
{
cout<<left<<setfill(' ')<<setw(10)<<++count
cout<<i<<" "<<j<<" "<<k<<endl
}
cout<<endl<<"共有"<<count<<"种组合"<<endl<<endl
}
已调试完毕,共3003条!!!每输出40条按任一键继续...,保证结果万无一失,你可以查看下,源程序如下:#include "stdio.h"
main()
{int i,j,k,m,n
long int count=0
for(i=1i<=15i++)
for(j=i+1j<=15j++)
for(k=j+1k<=15k++)
for(m=k+1m<=15m++)
for(n=m+1n<=15n++)
{if(iden(i,j,k,m,n))
printf("No %ld : %5d%5d%5d%5d%5d\n",++count,i,j,k,m,n)
if(count%45==0) getchar()
}
}
iden(int i,int j,int k,int m,int n)
{if(i==j||i==k||i==m||i==n||j==k||j==m||j==n||k==m||k==n||m==n)
return 0
else
return 1
}
再增加两个变量,修改下程序可以35选5,我机子是P4 cpu1.8G 内存是512M,跑了好久,估计要两个小时左右!!!没耐心等下去,有朋友想试下的我把程序弄下面来了!!
#include "stdio.h"
main()
{int i,j,k,m,n,q,w
long int count=0
for(i=1i<=35i++)
for(j=i+1j<=35j++)
for(k=j+1k<=35k++)
for(m=k+1m<=35m++)
for(n=m+1n<=35n++)
for(q=n+1q<=35q++)
for(w=q+1w<=35w++)
{if(iden(i,j,k,m,n,q,w))
printf("No %ld : %5d%5d%5d%5d%5d%5d%5d\n",++count,i,j,k,m,n,q,w)
if(count%45==0) getchar()
}
}
iden(int i,int j,int k,int m,int n,int q,int w)
{if(i==j||i==k||i==m||i==n||i==q||i==w||j==k||j==m||j==n||j==q||j==w||k==m||k==n||k==q||k==w||m==n||m==q||m==w||n==q||n==w||q==w)
return 0
else
return 1
}
void f(){
int i1,i2,i3,i4,i5,i6,i7,i8
for (i1=1i1<32i1++)
for (i2=1i2<32i2++){if (i1==i2) continue
for (i3=1i3<32i3++){if (i1==i3) continueif (i2==i3) continue
for (i4=1i4<32i4++){if (i1==i4) continueif (i2==i4) continueif (i3==i4) continue
for (i5=1i5<32i5++){if (i1==i5) continueif (i2==i5) continueif (i3==i5) continueif (i4==i5) continue
for (i6=1i6<32i6++){if (i1==i6) continueif (i2==i6) continueif (i3==i6) continueif (i4==i6) continueif (i5==i6) continue
for (i7=1i7<32i7++){if (i1==i7) continueif (i2==i7) continueif (i3==i7) continueif (i4==i7) continueif (i5==i7) continueif (i6==i7) continue
for (i8=1i8<32i8++){if (i1==i8) continueif (i2==i8) continueif (i3==i8) continueif (i4==i8) continueif (i5==i8) continueif (i6==i8) continueif (i7==i8) continue
printf("%d,%d,%d,%d,%d,%d,%d,%d\n",i1,i2,i3,i4,i5,i6,i7,i8)
}}}}}}}
}
不过估计没有什么用,因为输出的组合数多达
31*30*29*28*27*26*25/(1*2*3*4*5*6*7*8)个
没有实际意义。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)