问题描述
刚接触c#求教下面问题如何编程编一个程序,从三个红球,五个白球,六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案。
解决方案
解决方案二:
两个For循环遍历即可?比如:for(inti=1;i<=5;i++){for(intj=1;j<=3;j++){if(i+j==8){Console.Writeline(“白球:i个;红球:j个”);}else{Console.Writeline("白球:i个;红球:j个;黑球:8-i-j个");}}}
解决方案三:
//白球可以是1-5个for(intwrite=1;write<=5;write++){//红球可以是0-3个for(intred=0;red<=3;red++){//剩下的就是黑球Console.WriteLine("write:"+write+"red:"+red+"black:"+(8-write-red));}}write:1red:0black:7write:1red:1black:6write:1red:2black:5write:1red:3black:4write:2red:0black:6write:2red:1black:5write:2red:2black:4write:2red:3black:3write:3red:0black:5write:3red:1black:4write:3red:2black:3write:3red:3black:2write:4red:0black:4write:4red:1black:3write:4red:2black:2write:4red:3black:1write:5red:0black:3write:5red:1black:2write:5red:2black:1write:5red:3black:0
解决方案四:
引用2楼crystal_lz的回复:
//白球可以是1-5个for(intwrite=1;write<=5;write++){//红球可以是0-3个for(intred=0;red<=3;red++){//剩下的就是黑球Console.WriteLine("write:"+write+"red:"+red+"black:"+(8-write-red));}}write:1red:0black:7write:1red:1black:6write:1red:2black:5write:1red:3black:4write:2red:0black:6write:2red:1black:5write:2red:2black:4write:2red:3black:3write:3red:0black:5write:3red:1black:4write:3red:2black:3write:3red:3black:2write:4red:0black:4write:4red:1black:3write:4red:2black:2write:4red:3black:1write:5red:0black:3write:5red:1black:2write:5red:2black:1write:5red:3black:0
....write....可能他还要求有排序什么的,想了有点久了。。。。
解决方案五:
上面贴的只是数据碰巧不会出现什么特殊情况如果有什么特殊情况的用这个intnCount=8;//总共拿出个数intnRedCount=100;intnWriteCount=100;intnBlackCount=1;for(intwrite=1;write<=nWriteCount&&write<=nCount;write++){for(intred=0;red<=nRedCount&&(write+red)<=nCount;red++){//黑球不够凑够8个的情况下的情况下if(nCount-write-red>nBlackCount)continue;Console.WriteLine("write:"+write+"red:"+red+"black:"+(nCount-write-red));}}write:1red:6black:1write:1red:7black:0write:2red:5black:1write:2red:6black:0write:3red:4black:1write:3red:5black:0write:4red:3black:1write:4red:4black:0write:5red:2black:1write:5red:3black:0write:6red:1black:1write:6red:2black:0write:7red:0black:1write:7red:1black:0write:8red:0black:0
解决方案六:
引用3楼hjq624779687的回复:
Quote: 引用2楼crystal_lz的回复:
//白球可以是1-5个for(intwrite=1;write<=5;write++){//红球可以是0-3个for(intred=0;red<=3;red++){//剩下的就是黑球Console.WriteLine("write:"+write+"red:"+red+"black:"+(8-write-red));}}write:1red:0black:7write:1red:1black:6write:1red:2black:5write:1red:3black:4write:2red:0black:6write:2red:1black:5write:2red:2black:4write:2red:3black:3write:3red:0black:5write:3red:1black:4write:3red:2black:3write:3red:3black:2write:4red:0black:4write:4red:1black:3write:4red:2black:2write:4red:3black:1write:5red:0black:3write:5red:1black:2write:5red:2black:1write:5red:3black:0....write....可能他还要求有排序什么的,想了有点久了。。。。
求都是一样还有什么排序?难道还要排列组合不成。。。再拿出来的球上面还有什么排法不成?。。
解决方案七:
引用5楼crystal_lz的回复:
Quote: 引用3楼hjq624779687的回复:
Quote: 引用2楼crystal_lz的回复:
//白球可以是1-5个for(intwrite=1;write<=5;write++){//红球可以是0-3个for(intred=0;red<=3;red++){//剩下的就是黑球Console.WriteLine("write:"+write+"red:"+red+"black:"+(8-write-red));}}write:1red:0black:7write:1red:1black:6write:1red:2black:5write:1red:3black:4write:2red:0black:6write:2red:1black:5write:2red:2black:4write:2red:3black:3write:3red:0black:5write:3red:1black:4write:3red:2black:3write:3red:3black:2write:4red:0black:4write:4red:1black:3write:4red:2black:2write:4red:3black:1write:5red:0black:3write:5red:1black:2write:5red:2black:1write:5red:3black:0....write....可能他还要求有排序什么的,想了有点久了。。。。
求都是一样还有什么排序?难道还要排列组合不成。。。再拿出来的球上面还有什么排法不成?。。
用C#还纠结排序?数据防到list里面,然后Sort啊....
解决方案八:
只要各个球没有编号就好,白球一定是{1,2,3,4,5},剩下考虑红黑的
解决方案九:
谢谢各位,已经懂了。
解决方案十:
19种组合、352种排列//编一个程序,从三个红球,五个白球,六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案。staticvoidMain(){intgIndex=0,aIndex=0;ConsoleColor[]colors=newConsoleColor[]{ConsoleColor.White,ConsoleColor.Red,ConsoleColor.DarkGray};for(intwhite=1;white<=5;white++){for(intred=0;red<=3;red++){intblack=8-white-red;if(black<0||black>6)continue;//白+红不足2个,抛弃gIndex++;//分别用0、1、2表示白球、红球、黑球int[]arry=newint[8];for(inti=0;i<white;i++)arry[i]=0;for(inti=0;i<red;i++)arry[i+white]=1;for(inti=0;i<black;i++)arry[7-i]=2;do{aIndex++;Console.Write("{0:00}:{1:000}:",gIndex,aIndex);for(inti=0;i<arry.Length;i++){Console.ForegroundColor=colors[arry[i]];Console.Write("●");}Console.WriteLine();Console.ResetColor();}while(Range(arry));}}Console.ReadKey();}privatestaticboolRange(int[]arry){for(inti=arry.Length-1;i>0;i--){if(arry[i-1]<arry[i]){inttmp=arry[i-1];arry[i-1]=arry[i];arry[i]=tmp;returntrue;}}returnfalse;}
解决方案十一:
引用9楼caojinrong的回复:
19种组合、352种排列//编一个程序,从三个红球,五个白球,六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案。staticvoidMain(){intgIndex=0,aIndex=0;ConsoleColor[]colors=newConsoleColor[]{ConsoleColor.White,ConsoleColor.Red,ConsoleColor.DarkGray};for(intwhite=1;white<=5;white++){for(intred=0;red<=3;red++){intblack=8-white-red;if(black<0||black>6)continue;//白+红不足2个,抛弃gIndex++;//分别用0、1、2表示白球、红球、黑球int[]arry=newint[8];for(inti=0;i<white;i++)arry[i]=0;for(inti=0;i<red;i++)arry[i+white]=1;for(inti=0;i<black;i++)arry[7-i]=2;do{aIndex++;Console.Write("{0:00}:{1:000}:",gIndex,aIndex);for(inti=0;i<arry.Length;i++){Console.ForegroundColor=colors[arry[i]];Console.Write("●");}Console.WriteLine();Console.ResetColor();}while(Range(arry));}}Console.ReadKey();}privatestaticboolRange(int[]arry){for(inti=arry.Length-1;i>0;i--){if(arry[i-1]<arry[i]){inttmp=arry[i-1];arry[i-1]=arry[i];arry[i]=tmp;returntrue;}}returnfalse;}
一共有20种排列组合有多少种我是不知道不过看你的打印来看下面几个应该就是三个红球的组合排法了可是我随便想了一种组合为什么没有看到?红红白白白白..红????
解决方案十二:
不好意思,算法有误,if(arry[i-1]<arry[i]){inttmp=arry[i-1];arry[i-1]=arry[i];arry[i]=tmp;returntrue;}
此处应该把i后面比第i个元素大的最小值和第i元素互换,然后将i+1之后的元素按小至大排列,要是纠结结果的话可以自选修改。
解决方案十三:
staticvoidMain(string[]args){intred=3;intwrite=5;intblack=6;for(inti=1;i<=write;i++){for(intj=0;j<=red;j++){black=8-i-j;if(black!=7)//黑球数只有6个{Console.WriteLine("write:{0}"+"red:{1}"+"black:{2}",i,j,black);}}}Console.ReadKey();}write:1red:1black:6write:1red:2black:5write:1red:3black:4write:2red:0black:6write:2red:1black:5write:2red:2black:4write:2red:3black:3write:3red:0black:5write:3red:1black:4write:3red:2black:3write:3red:3black:2write:4red:0black:4write:4red:1black:3write:4red:2black:2write:4red:3black:1write:5red:0black:3write:5red:1black:2write:5red:2black:1write:5red:3black:0