3对情侣参加婚礼,3个新郎为A,B,C,3个新娘为 X,Y,Z,有人想知道究竟谁和谁结婚,于是问新人中的三位,得到如下结果
A说他将和X结婚
X说她的未婚夫是C
C说他将和Z结婚
但他们说的都是假话,求他们谁和谁结婚
#include <stdio.h> int main(void) { char male[3] = {'a','b','c'}; char female[3] = {'x','y','z'}; int passble[3][3] = {{0,0,0},{0,0,0},{0,0,0}}; int male_count = sizeof(male); int female_count = sizeof(female); int result = 0; int i = 0; int n = 0; for(i;i<male_count;i++) { n = 0; for(n;n<female_count;n++) { if(male[i] == 'a' && female[n] == 'x') continue; if(male[i] == 'c' && female[n] == 'x') continue; if(male[i] == 'c' && female[n] == 'z') continue; passble[i][n] = 1; } } result = process(female_count,male_count,passble); if(result == 1){ for(i = 0;i<male_count;i++){ printf("del %d %d",n,temp_int); for(n = 0;n<female_count;n++) { if(passble[i][n] == 1){ printf("%c -- %c\n",male[i],female[n]); } } } } return 0; } int process(int female_count,int male_count,int passble[3][3]) { int i,n,temp_int,passble_count,is_ok = 1; for(i = 0;i<male_count;i++) { passble_count = 0; for(n = 0;n<female_count;n++) { if(passble[i][n]) { temp_int = n; ++passble_count; } } if(passble_count == 1){ for(n = 0;n<female_count;n++){ if(n == i) continue; passble[n][temp_int] = 0; } } if(passble_count < 1 ) is_ok = 2; if(passble_count > 1 ) is_ok = 0; } if(is_ok == 0) process(female_count,male_count,passble); if(is_ok == 1) return 1; if(is_ok == 2) return 2; }