|
发表于 2004-9-12 23:51:47
|
显示全部楼层
下面是正确的代码:
[code:1]
main()
{
enum color { red, yellow, blue, white, black };
enum color i, j, k, pri;
int n, loop;
n = 0;
for (i = red; i <= black; i++)
for (j = i + 1; j <= black; j++)
if (i != j) {
for (k = j + 1; k <= black; k++)
if ((k != i) && (k != j)) {
n = n + 1;
printf("%-4d", n);
for (loop = 1; loop <= 3; loop++) {
switch (loop) {
case 1:
pri = i;
break;
case 2:
pri = j;
break;
case 3:
pri = k;
break;
default:
break;
}
switch (pri) {
case red:
printf("%-10s", "red");
break;
case yellow:
printf("%-10s", "yellow");
break;
case blue:
printf("%-10s", "blue");
break;
case white:
printf("%-10s", "white");
break;
case black:
printf("%-10s", "black");
break;
default:
break;
}
}
printf("\n");
if (n > 50) exit(0);
}
}
printf("\ntotal:%5d\n", n);
}
[/code:1]
其中,两个比较
if (i != j)
if ((k != i) && (k != j))
可以不要
执行的结果如下:
[code:1]
[hyh@bzimage ctmp]$ ./test5
1 red yellow blue
2 red yellow white
3 red yellow black
4 red blue white
5 red blue black
6 red white black
7 yellow blue white
8 yellow blue black
9 yellow white black
10 blue white black
total: 10
[/code:1] |
|