一、题目
新郎A,B,C。新娘X,Y,Z。
A说他将和X结婚,X说她将和C结婚,C说他将和Z结婚。这三句全是假的。请问真正是怎么配对‘
二、分析
可以用穷举法,一共3+2+1种可能方案。
A,B,C位置不变,不断调换X,Y,Z的位置。
三、源码
1: #include <stdio.h>
2:
3: int match(int i, int j, int k, char wife[]);
4:
5: int main()
6: {
7: int i, j ,k;
8: char husband[3] = {'A', 'B', 'C'};
9: char wife[3] = {'X', 'Y', 'Z'};
10:
11: for(i=0; i<3; i++)
12: for(j=0; j<3; j++)
13: for(k=0; k<3; k++)
14: if (i!=j && j!=k && i!=k)
15: {
16: if (match(i,j,k,wife))
17: {
18: printf("husband wife\n");
19: printf("A----------%c\n", wife[i]);
20: printf("B----------%c\n", wife[j]);
21: printf("C----------%c\n", wife[k]);
22: }
23: }
24: return 0;
25: }
26:
27: int match(int i, int j, int k, char wife[])
28: {
29: if (wife[i] == 'X')
30: return 0;
31: if (wife[k] == 'X')
32: return 0;
33: if ( wife[k] == 'Z')
34: return 0;
35:
36: return 1;
37: }