恋爱配对算法请教,请教 请教

恋爱配对算法请教,请教 请教

img

#include
#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
int main() {
    int max = 0, max1 = 0,m = 0, n = 0,k = 0;
    cin >> k;
    int** p = (int**)malloc(k * sizeof(int*));
    int** q = (int**)malloc(k * sizeof(int*));
    for (int i = 0; i < k; i++) {
        p[i] = (int*)malloc(k * sizeof(int));
        q[i] = (int*)malloc(k * sizeof(int));
    }
    for (int i = 0; i < k; i++) {
        for (int j = 0; j < k; j++) {
            scanf("%d", &p[i][j]);
        }
    }
    for (int i = 0; i < k; i++) {
        for (int j = 0; j < k; j++) {
            scanf("%d", &q[i][j]);
        }
    }
    for (int count = 0; count < k;count++) {
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < k; j++) {
                if (p[i][j] * q[i][j] > max1) {
                    max1 = p[i][j] * q[i][j];
                    m = i;
                    n = j;
                }
            }
        }
        for (int i = 0; i < k; i++) {
            p[m][i] = 0;
            q[m][i] = 0;
            p[i][n] = 0;
            q[i][n] = 0;
        }
        max += max1;
        max1 = 0;
    }
    cout << max;
}


感觉贪心算法是有瑕疵的,但又不知道该怎么改。请教一下


#include <iostream>
#include <vector>
#include <string>
 
using namespace std;
 
struct Person {
    string name;
    int age;
    string gender;
    vector<string> interests;
};
 
vector<Person> match(vector<Person> people) {
    vector<Person> matches;
    for (int i = 0; i < people.size(); i++) {
        Person p1 = people[i];
        for (int j = i + 1; j < people.size(); j++) {
            Person p2 = people[j];
            if (p1.gender != p2.gender && p1.age == p2.age) {
                bool match = true;
                for (int k = 0; k < p1.interests.size(); k++) {
                    if (find(p2.interests.begin(), p2.interests.end(), p1.interests[k]) == p2.interests.end()) {
                        match = false;
                        break;
                    }
                }
                if (match) {
                    matches.push_back(p1);
                    matches.push_back(p2);
                }
            }
        }
    }
    return matches;
}
 
int main() {
    vector<Person> people;
    // Add people to the vector
    vector<Person> matches = match(people);
    for (int i = 0; i < matches.size(); i += 2) {
        cout << matches[i].name << " and " << matches[i + 1].name << " are a match!" << endl;
    }
    return 0;
}