编一个程序,从三个红球,五个白球,六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案

问题描述

刚接触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

时间: 2024-12-27 10:00:06

编一个程序,从三个红球,五个白球,六个黑球中任意取出八个球,且其中必须有白球,输出所有可能的方案的相关文章

老外编的程序(三)--正则表达式的Demo

程序|正则 using System.Text.RegularExpressions; class Test {    static string CapText(Match m) {    // get the matched string    string x = m.ToString();    // if the first char is lower case    if (char.IsLower(x[0])) {        // capitalize it        re

编一个程序

问题描述 文件data.txt如下:abfed14fhemk7diloqdkmld2d将其中的元素按如下规则互换:对角互换(如a[1][1]=a[5][5],a[1][5]=a[5][1]...)然后写入到data1.txt中 解决方案 解决方案二:a[5][5]???你强.解决方案三:引用楼主vitamin11的帖子: 文件data.txt如下:abfed14fhemk7diloqdkmld2d将其中的元素按如下规则互换:对角互换(如a[1][1]=a[5][5],a[1][5]=a[5][1

如何编一个电算程序?

问题描述 如何编一个电算程序? 我们需要一个电算程序来计算,主要的就是利用迭代法来算,如果手算过于麻烦,所以我们老师要求我们编一个程序来算,要求了精度和输出结果是表格,我不会.

编一个应用程序是C#加SQL的,编完后安装这个程序的电脑上必须装SQL并且要建立相应的数据库吗?

问题描述 编一个应用程序是C#加SQL的,编完后安装这个程序的电脑上必须装SQL并且要建立相应的数据库吗? 解决方案 解决方案二:不用,按上MDAC就可以了解决方案三:数据库是SQLSERVER吗?是的话,必须要有一台机安装

DB2编程序技巧 (三)_DB2

正在看的db2教程是:DB2编程序技巧 (三).  另一种为       pcursor1: for loopcs1 as  cousor1  cursor  as select  market_code  as market_code            from tb_market_code            for update         do         end for;        这种方式的优点是比较简单,不用(也不允许)使用open,fetch,close.   

急急急 随意编一个使用程序 谢谢!

问题描述 帮我随意编一个C#使用程序实现简单的时钟之类功能的就好谢谢了~~~~ 解决方案 解决方案二:www.codeproject.com随便找下就找到了解决方案三:给:年.月.日.时.分.秒.毫秒stringNian=DateTime.Now.Year.ToString(),Yue=DateTime.Now.Month.ToString(),Ri=DateTime.Now.Day.ToString(),shi=DateTime.Now.Hour.ToString(),fenzhfong=Da

c语言字符串-编一个跟字符串有关的C语言程序

问题描述 编一个跟字符串有关的C语言程序 首先要定义2个数组 然后输入2个字符串 再分别求出字符串的长度 接着比较2个字符串的大小 最后链接在一起 下面是我自己弄的,请看看 #include #include int main() { char q[50],p[10]; gets(q); gets(p); printf("q的字符串长度为%dn",strlen(q)); printf("p的字符串长度为%dn",strlen(p)); printf("%d

如何用vb编一个能随机出选择题并判分的程序?

问题描述 如何用vb编一个能随机出选择题并判分的程序? 我有套题存放在word中,我想用这套提中的一些随机出一张卷并能作答然后出分. 解决方案 word并不是存储题目的好方式.如果你非要这么做,你首先必须明确,你的题目以什么方式存放.比如说,每个题目由什么文字开头,或者题目和题目之间的分隔符是什么. 否则你的word文档把题目混合在一起,根本分不出题目和题目,题干.选择项.答案.后面的一系列问题也就没法解决了. 建议你把这套题目先放在数据库的表中,每行记录包括题号.题干.N个选项.答案等信息.

求解答-试编写一个算法,找出一个循环链表中的最小值。我是新手,编了一个程序,不知错在哪

问题描述 试编写一个算法,找出一个循环链表中的最小值.我是新手,编了一个程序,不知错在哪 #includeusing namespace std; class LinkNode{ int data; LinkNode *link; LinkNode(int d=0LinkNode *l=0){data=d;link=l;}}; class List{private: LinkNode *first; int n;public: List() { first=new LinkNode; first