程序代码:
#include <stdio.h> struct teacher { int n1; /* 课程编号,默认 n1<n2 */ int n2; }; int check(struct teacher T) { /* 教语文的不教算术、音乐和地理 */ if (T.n1 == 1 || T.n2 == 1){ if (T.n1 == 3 || T.n2 == 3 ||\ T.n1 == 5 || T.n2 == 5 ||\ T.n1 == 4 || T.n2 == 4) return -1; } /* 教地理的不教算术 */ if (T.n1 == 4 || T.n2 == 4) { if (T.n1 == 1 || T.n2 == 1||\ T.n1 == 3 || T.n2 == 3) return -1; } return 0; } int main() { int i, j, m, n, k; struct teacher A, B, C; for (i=1;i<6;i++) { for (j = i+1; j < 7; j++) { A.n1 = i; A.n2 = j; /* A不教算术和地理 */ if (A.n1 == 3|| A.n1 == 4|| A.n2 == 3|| A.n2 == 4) continue; for(m=1;m<6;m++) for(n=m+1; n<7; n++) { B.n1 = m; B.n2 = n; /* B不教地理,音乐,语文 */ if (B.n1 == 1||B.n1 == 4||B.n1 == 5 ||\ B.n2 == 1||B.n2 == 4||B.n2 == 5) { continue; } if ( (m==i||m==j) ||(n==i||n==j)) continue; for(k=1;k<7;k++) if (k != i && k != j && k != m && k != n) { C.n1 = k; C.n2 = 1+2+3+4+5+6-m-n-i-j-k; break; } /* 检查是否符合题目条件 */ if ( check(A) || check(B) || check(C) ){ continue; } else { /* 输出 */ printf("A %d %d\n", A.n1, A.n2); printf("B %d %d\n", B.n1, B.n2); printf("C %d %d\n\n", C.n1, C.n2); } }/* end while m*/ }/* end while j */ }/* end while i*/ return 0; } 最后得出两组,不知道是不是对的。
好好学习,天天想上