书P111:
常量列表里都是整型变量
验证:
好吧,实型确实不能输入
枚举元素按常量处理,不能赋初值,sun=0;这样的语句是非法的。
他这个“sun=0;这样的语句”到底在具体程序中呈现(展现)的是什么语句?
如果我在电脑里输入:
enum weekday { sun = 7, mon = 1, tue, wed, thu, fri, sat };
sun = 0;
里面的sun连类型都没有指定,更何(况)来赋值呢?
这个语句语序根本就是有病的嘛,怎么可能输出什么有用的东西,肯定报错
例7-2
口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中任意取出3个球
问得到3种不同颜色的球的可能取法,输出每种排列的情况。
源程序:
#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++)//当 i为某一颜色时
for (j = red; j <= black; j++)//当j为某一颜色时
if (i != j)//若前两个球的颜色不同
{
for (k = red; k <= black; k++)
//只有前两个球的颜色不同才需要检查第3个球的颜色
if ((k != i) && (k != j))//3个球的颜色都不同
{
n = n + 1;//使累计值n加1
cout << setw(3) << n;//输出当前的n值,字段宽度为3
for (loop = 1; loop <= 3; loop++)
//先后对3个球作处理
{
switch (loop)//loop的值先后为1、2、3
{
case 1: pri = color(i); break;
//color(i)是强制类型转换,使pri的值为i
case 2: pri = color(j); break;//使pri的值为j
case 3: pri = color(k); break;//使pri的值为k
default: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;//输出符合条件的组合的个数
return 0;
}
该程序具体运行设计流程步骤:
变量pri:
用于在不同时间作为存储i,j,k不同变量不同的值的容器(变量)
结果:
- 1 red yellow blue
- 2 red yellow white
- 3 red yellow black
- 4 red blue yellow
- 5 red blue white
- 6 red blue black
- 7 red white yellow
- 8 red white blue
- 9 red white black
- 10 red black yellow
- 11 red black blue
- 12 red black white
- 13 yellow red blue
- 14 yellow red white
- 15 yellow red black
- 16 yellow blue red
- 17 yellow blue white
- 18 yellow blue black
- 19 yellow white red
- 20 yellow white blue
- 21 yellow white black
- 22 yellow black red
- 23 yellow black blue
- 24 yellow black white
- 25 blue red yellow
- 26 blue red white
- 27 blue red black
- 28 blue yellow red
- 29 blue yellow white
- 30 blue yellow black
- 31 blue white red
- 32 blue white yellow
- 33 blue white black
- 34 blue black red
- 35 blue black yellow
- 36 blue black white
- 37 white red yellow
- 38 white red blue
- 39 white red black
- 40 white yellow red
- 41 white yellow blue
- 42 white yellow black
- 43 white blue red
- 44 white blue yellow
- 45 white blue black
- 46 white black red
- 47 white black yellow
- 48 white black blue
- 49 black red yellow
- 50 black red blue
- 51 black red white
- 52 black yellow red
- 53 black yellow blue
- 54 black yellow white
- 55 black blue red
- 56 black blue yellow
- 57 black blue white
- 58 black white red
- 59 black white yellow
- 60 black white blue
- total:60
我觉得写的时候没必要搞那么复杂:
#include
using namespace std;
int main()
{
enum color { red, yellow, blue, white, black }; //声明枚举类型 color
color pri;//定义color 类型的变量pri
int i, j, k,l, n = 0; //n是累计不同颜色的组合数
for (i = red; i <= black; i++)//当 i为某一颜色时
for (j = red; j <= black; j++)//当j为某一颜色时
if (i != j)//若前两个球的颜色不同
{
for (k = red; k <= black; k++)
//只有前两个球的颜色不同才需要检查第3个球的颜色
if ((k != i) && (k != j))//3个球的颜色都不同
{
n = n + 1;//使累计值n加1
cout << n;//输出当前的n值,字段宽度为3
for (l = 1; l <= 3; l++)
//先后对3个球作处理
{
switch (l)//loop的值先后为1、2、3
{
case 1: pri = color(i); break;
//color(i)是强制类型转换,使pri的值为i
case 2: pri = color(j); break;//使pri的值为j
case 3: pri = color(k); break;//使pri的值为k
default:break;
}
switch (pri) //判断pri的值,输出相应的“颜色”
{
case red: cout << " red "; break;
case yellow: cout << " yellow "; break;
case blue:cout << " blue "; break;
case white:cout << " white "; break;
case black:cout << " black "; break;
default: break;
}
}
cout << endl;
}
}
cout << "total:" << n << endl;//输出符合条件的组合的个数
return 0;
}
改动处:
结果:
- 1 red yellow blue
- 2 red yellow white
- 3 red yellow black
- 4 red blue yellow
- 5 red blue white
- 6 red blue black
- 7 red white yellow
- 8 red white blue
- 9 red white black
- 10 red black yellow
- 11 red black blue
- 12 red black white
- 13 yellow red blue
- 14 yellow red white
- 15 yellow red black
- 16 yellow blue red
- 17 yellow blue white
- 18 yellow blue black
- 19 yellow white red
- 20 yellow white blue
- 21 yellow white black
- 22 yellow black red
- 23 yellow black blue
- 24 yellow black white
- 25 blue red yellow
- 26 blue red white
- 27 blue red black
- 28 blue yellow red
- 29 blue yellow white
- 30 blue yellow black
- 31 blue white red
- 32 blue white yellow
- 33 blue white black
- 34 blue black red
- 35 blue black yellow
- 36 blue black white
- 37 white red yellow
- 38 white red blue
- 39 white red black
- 40 white yellow red
- 41 white yellow blue
- 42 white yellow black
- 43 white blue red
- 44 white blue yellow
- 45 white blue black
- 46 white black red
- 47 white black yellow
- 48 white black blue
- 49 black red yellow
- 50 black red blue
- 51 black red white
- 52 black yellow red
- 53 black yellow blue
- 54 black yellow white
- 55 black blue red
- 56 black blue yellow
- 57 black blue white
- 58 black white red
- 59 black white yellow
- 60 black white blue
- total:60
这不也感觉上差不多嘛
如果想简单一点的编写程序:
#include
using namespace std;
int main()
{
enum color { red, yellow, blue, white, black }; //声明枚举类型 color
color pri;//定义color 类型的变量pri
int i, j, k,l, n = 0; //n是累计不同颜色的组合数
for (i = red; i <= black; i++)//当 i为某一颜色时
for (j = red; j <= black; j++)//当j为某一颜色时
if (i != j)//若前两个球的颜色不同
for (k = red; k <= black; k++)
//只有前两个球的颜色不同才需要检查第3个球的颜色
if ((k != i) && (k != j))//3个球的颜色都不同
{
n = n + 1;//使累计值n加1
cout << n;//输出当前的n值,字段宽度为3
cout <<" " << i << " "
<< j << " "
<< k << " " << endl;
}
cout << "total:" << n << endl;//输出符合条件的组合的个数
return 0;
}
结果:
- 1 0 1 2
- 2 0 1 3
- 3 0 1 4
- 4 0 2 1
- 5 0 2 3
- 6 0 2 4
- 7 0 3 1
- 8 0 3 2
- 9 0 3 4
- 10 0 4 1
- 11 0 4 2
- 12 0 4 3
- 13 1 0 2
- 14 1 0 3
- 15 1 0 4
- 16 1 2 0
- 17 1 2 3
- 18 1 2 4
- 19 1 3 0
- 20 1 3 2
- 21 1 3 4
- 22 1 4 0
- 23 1 4 2
- 24 1 4 3
- 25 2 0 1
- 26 2 0 3
- 27 2 0 4
- 28 2 1 0
- 29 2 1 3
- 30 2 1 4
- 31 2 3 0
- 32 2 3 1
- 33 2 3 4
- 34 2 4 0
- 35 2 4 1
- 36 2 4 3
- 37 3 0 1
- 38 3 0 2
- 39 3 0 4
- 40 3 1 0
- 41 3 1 2
- 42 3 1 4
- 43 3 2 0
- 44 3 2 1
- 45 3 2 4
- 46 3 4 0
- 47 3 4 1
- 48 3 4 2
- 49 4 0 1
- 50 4 0 2
- 51 4 0 3
- 52 4 1 0
- 53 4 1 2
- 54 4 1 3
- 55 4 2 0
- 56 4 2 1
- 57 4 2 3
- 58 4 3 0
- 59 4 3 1
- 60 4 3 2
- total:60
剩下只要在补充一个把“i,j,k”从数字转化为颜色的模块即可