C++列舉型別實驗:三色球問題
阿新 • • 發佈:2019-02-08
口袋中有紅、黃、藍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; }