C++枚举类型实验:三色球问题
口袋中有红、黄、蓝3种颜色的小球,如果每次从口袋中取出2种不同颜色的小球,编写程序,打印出每种组合。
#include <iostream>
#include <string>
using namespace std;
int main()
{
enum color { red, yellow, blue };
int temp, i, j;
for (i = red; i <= yellow; i++)
{
for (j = i + 1; j <= blue; j++)
{
for (int t = 0; t<2; t++)//控制输出
{
switch (t)
{
case 0: temp = i; break;
case 1: temp = j; break;
}
switch ((enum color)temp)
{
case red: cout << "red" << "\t"; break;
case yellow: cout << "yellow" << "\t"; break;
case blue: cout << "blue" << "\t"; break;
}
}
cout << "\n";
}
}
return 0;
}