口袋中有5种颜色的小球,每次从口袋中任意取3个,问得到3种不同颜色的小球的可能取法?
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
using namespace std;
int main()
{
enum color{red,yellow,blue,white,black};//声明枚举类型color
color pri; //定义color类型的变量pri
int i, j, k,n = 0, loop; //n是累计不同颜色的组合数
for (i = red; i <= black; i++)
{
for (j = red; j <= black;j++)
if (i != j)
{
for (k = red; k <= black;k++)
if ((k != i) && (k != j))
{
n = n + 1;
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;
defaul:break;
}
switch (pri)//判断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 << "total:" << n << endl;//输出符合条件的组合的个数
getchar();
return 0;
}
新闻名称:利用枚举类型实现统计
分享链接:
http://bzwzjz.com/article/gpghsd.html