1269 - C语言11.5
口袋中有红、黄、蓝、白、黑5种颜色的球若干个。每次从口袋中先后取出3个球,问得到3种不同颜色的球的可能取法,并输出每一种排列的情况。
要求使用枚举类型来完成本题。
题目输入
无
题目输出
对于每一种排列的情况,在一行内输出所选的三种颜色,用空格隔开。
在输出所有排列的情况之后,在最后一行输出排列的情况总数。
请注意行尾输出换行。
输入/输出样例
题目输入
无
题目输出
red yellow blue red yellow white red yellow black red blue yellow red blue white red blue black red white yellow red white blue red white black red black yellow red black blue red black white yellow red blue yellow red white yellow red black yellow blue red yellow blue white yellow blue black yellow white red yellow white blue yellow white black yellow black red yellow black blue yellow black white blue red yellow blue red white blue red black blue yellow red blue yellow white blue yellow black blue white red blue white yellow blue white black blue black red blue black yellow blue black white white red yellow white red blue white red black white yellow red white yellow blue white yellow black white blue red white blue yellow white blue black white black red white black yellow white black blue black red yellow black red blue black red white black yellow red black yellow blue black yellow white black blue red black blue yellow black blue white black white red black white yellow black white blue 60
C语言解答
#include <stdio.h> enum color { red, yellow, blue, white, black }; int main() { void output(enum color x); enum color i, j, k; int count = 0; for (i = red;i <= black;i++) { for (j = red;j <= black;j++) { if (i != j) { for (k = red;k <= black;k++) { if (i != k && j != k) { output(i); printf(" "); output(j); printf(" "); output(k); printf("\n"); count++; } } } } } printf("%d\n", count); return 0; } /* 将枚举类型转换为相应的字符串进行输出 */ void output(enum color x) { switch (x) { case red: printf("red"); break; case yellow: printf("yellow"); break; case blue: printf("blue"); break; case white: printf("white"); break; case black: printf("black"); break; } }
C++解答
#include<iostream> #include<iomanip> #include<fstream> #include<string> #include<climits> #include<cctype> #include<cmath> using namespace std; int main() { //ifstream cin("aaa.txt"); int i,j,n,m,k,sum; string s[5]={"red","yellow","blue","white","black"}; sum=0; for(i=0;i<5;i++) for(j=0;j<5;j++) for(k=0;k<5;k++) if(i!=j && i!=k && j!=k) {cout<<s[i]<<" "<<s[j]<<" "<<s[k]<<endl;sum++;} cout<<sum<<endl; return 0; }