好的,所以我正式在我的智慧结束。我正在让这个程序编译并运行几乎完成。缺少的最后一件事是正确返回(粗略地说,将 i 设置为 0 以重新测试所有单身男性并尝试正确配对)。这是我的算法。我不确定为什么我的 while 循环结束不起作用。在测试所有男人之后,如果满足某人单身的条件,是否有更好的方法尝试将“i”设置回0?
任何事情都会有很大的帮助。这是应该发生的事情的图片: 单击
//Check availability
i=0;
while ( i < ROWS /*(checkStatus(manStatus) && checkStatus(womanStatus))*/ ) {
//Loop through man #"i"
//for(i=0; i< ROWS ; i++) {
if(manStatus[i] == -1) /*if man i is free, then:*/ {
for(w=0; w<COLS; w++) {
//Assign him the first woman on his list that is free:
if (womanStatus[mPref[i][w+1]] == -1) { //if the woman in question is free (i.e -1 value), then :
E[i] = mPref[i][w+1]; //Row i, starting from index 1;
printf("Man #%d proposes to Woman#%d\n", i, mPref[i][w+1]);
printf("Woman #%d is single, and accepts Man #%d\n\n", mPref[i][w+1], i);
manStatus[i] = 0;
womanStatus[mPref[i][w+1]] = 0;
//Print the Women's Availability
for(j=0; j<ROWS; j++) {
printf("Woman #%d is currently:%d\n",j, womanStatus[j]);
}
//Print Men's Availability
for(j=0; j<ROWS; j++) {
printf("Man #%d is currently:%d\n",j, manStatus[j]);
}
printf("\nMan #%d is now engaged to Woman #%d\n", i, mPref[i][w+1]);
i++;
break;
}
//If the woman he wants is taken, check to see if she likes him more than her current engagement:
if(womanStatus[mPref[i][w+1]] != -1) {
printf("Man #%d proposes to Woman#%d\n", i, mPref[i][w+1]);
printf("Sorry, Woman #%d is taken. ", mPref[i][w+1]);
//Check to see who the other guy is:
printf("She is married to Man #%d\n", E[mPref[i][w+1]]);
int oldGuy = E[mPref[i][w+1]];
int newGuy = i;
//printf("New Guy: %d\n", newGuy);
//If the newGuy comes first in the array, replace the old guy with newGuy
if(preferanceCheck(mPref, wPref, oldGuy, newGuy, mPref[i][w+1])) { //If this returns true, then new guy is better liked
//Set newGuy as w's current engagement, and then set oldGuy to -1
printf("Since Woman #%d prefers Man#%d, they get engaged!\n", mPref[i][w+1], newGuy);
E[i] = mPref[i][w+1];
manStatus[i] = 0;
manStatus[oldGuy] = -1;
womanStatus[mPref[i][w+1]] = 0;
printf("\nMan #%d is now engaged to Woman #%d\n", i, mPref[i][w+1]);
printf("Man #%d is now single\n\n", oldGuy);
//Print the Women's Availability
for(j=0; j<ROWS; j++) {
printf("Woman #%d is currently:%d\n",j, womanStatus[j]);
}
//Print Men's Availability
for(j=0; j<ROWS; j++) {
printf("Man #%d is currently:%d\n",j, manStatus[j]);
}
i++;
break;
}
}
}
printf("\n");
//Test to see if people are still single
for(j = 0; j < ROWS; j++) {
if(manStatus[j] == -1) {
printf("Sorry, but Man#%d is still single\n", j);
if(ROWS - i == 1) {
i=0;
}
}
if(womanStatus[j] == -1) {
printf("Sorry, but Woman#%d is still single\n", j);
if(ROWS - i == 1) {
i=0;
}
}
}
}
} //While Condition