c9-12.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #include <stdio.h>
  2. int main()
  3. {enum Color {red,yellow,blue,white,black}; //声明枚举类型enum Color
  4. enum Color i,j,k,pri; //定义枚举变量i,j,k,pri
  5. int n,loop;
  6. n=0;
  7. for (i=red;i<=black;i++) //外循环使i的值从red变到black
  8. for (j=red;j<=black;j++) //中循环使j的值从red变到black
  9. if (i!=j) //如果二球不同色
  10. { for (k=red;k<=black;k++) //內循环使k的值从red变到black
  11. if ((k!=i) && (k!=j)) //如果3球不同色
  12. {n=n+1; //符合条件的次数加1
  13. printf("%-4d",n); //输出当前是第几个符合条件的组合
  14. for (loop=1;loop<=3;loop++) //先后对三个球分别处理
  15. {switch (loop) //loop的值从1变到3
  16. {case 1: pri=i;break; //loop的值为1时,把第1球的颜色赋给pri
  17. case 2: pri=j;break; //loop的值为2时,把第2球的颜色赋给pri
  18. case 3: pri=k;break; //loop的值为3时,把第3球的颜色赋给pri
  19. default:break;
  20. }
  21. switch (pri) //根据球的颜色输出相应的文字
  22. {case red:printf("%-10s","red"); break; //pri的值等于枚举常量red时输出“red”
  23. case yellow: printf("%-10s","yellow"); break; //pri的值等于枚举常量yellowd时输出“yellow”
  24. case blue: printf("%-10s","blue"); break; //pri的值等于枚举常量blue时输出“blue”
  25. case white: printf("%-10s","white"); break; //pri的值等于枚举常量white时输出“white”
  26. case black: printf("%-10s","black"); break; //pri的值等于枚举常量black时输出“black”
  27. default :break;
  28. }
  29. }
  30. printf("\n");
  31. }
  32. }
  33. printf("\ntotal:%5d\n",n);
  34. return 0;
  35. }