例7.7:在口袋中有红,黄,蓝,白黑5种颜色的球若干,每次从口袋中取出3个球,问得到3种不同颜色的球的可能取法,打印出每种组合的3种颜色

//五种颜色的小球,枚举:(无序排列组合)
#include
#include
using namespace std;
enum color {red,yellow,blue,white,black}; //声明枚举类型

int main()
{
color pri;
int i, j, k, loop;
int n = 0; //n计数
for (i = red; i <= black; i++)
for (j = red; j <= black; j++)
for (k = red; k <= black; k++)
if ( i != j && i != k && j != k )
{
cout << setw(3) << ++n<<" " ;
for (loop = 1; loop <= 3; loop++)
{
switch (loop)
{
case 1:pri = color(i); break;
case 2:pri = color(j); break;
case 3:pri = color(k); break;
default:break;
}

					switch (pri)
					{
					case red:    cout << setw(8) << "red"<<"  ";      break;
					case yellow: cout << setw(8) << "yellow" << "  "; break;
					case blue:   cout << setw(8) << "blue" << "  ";   break;
					case white:  cout << setw(8) << "white" << "  ";  break;
					case black:  cout << setw(8) << "black" << "  ";  break;
					default: break;
					}
				}
				cout << endl;
			}	
		
cout << endl<<"total:"<<n<<endl;
return 0;

}

//五种颜色的小球,枚举:(有序排列)
#include
#include
using namespace std;

enum color { red, yellow, blue, white, black }; //声明枚举类型

int main()
{
color pri;
int i, j, k, loop;
int n = 0; //n计数
for (i = red; i <= blue; i++)
for (j = i+1; j <= white; j++)
for (k = j+1; k <= black; k++)
{
cout << setw(3) << ++n << " ";
for (loop = 1; loop <= 3; loop++)
{
switch (loop)
{
case 1:pri = color(i); break; //pri是color类型,i是int 类型,必须要强制转换类型!!!
case 2:pri = color(j); break;
case 3:pri = color(k); break;
default:break;
}

					switch (pri)
					{
					case red:    cout << setw(8) << "red" << "  ";      break;
					case yellow: cout << setw(8) << "yellow" << "  "; break;
					case blue:   cout << setw(8) << "blue" << "  ";   break;
					case white:  cout << setw(8) << "white" << "  ";  break;
					case black:  cout << setw(8) << "black" << "  ";  break;
					default: break;
					}
				}
				cout << endl;
			}

cout << endl << "total:" << n << endl;
return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值