Untitled

mail@pastecode.io avatar
unknown
plain_text
19 days ago
3.2 kB
6
Indexable
Never
Group* splitGroups(Member* const members, int numMembers, int standardLevel) {
    if (numMembers == 0) return NULL;
    // Sort members by happinessLevel in descending order
    qsort(members, numMembers, sizeof(Member), compareMembers);

    Group* groups = (Group* )malloc(numMembers * sizeof(Group));
    int nGroup = 0;
    int *added = malloc(numMembers * sizeof(int));
        //Khởi tạo mảng added
    for(int k = 0; k < numMembers; k++){added[k] = 0;}
    for (int i = 0; i < numMembers; i++) {

        groups[nGroup].members = (Member*)malloc(numMembers * sizeof(Member));
        groups[nGroup].numMembers = 0;
        groups[nGroup].happinessLevel = 0;

        for (int j = 0; j < numMembers; j++) {
            if(added[j] == 1) continue;
            if(groups[nGroup].happinessLevel + members[j].happinessLevel >= standardLevel){
                //Nếu không còn thằng nào cộng lại lớn hơn

                int nextIdx = getNextMemberNotAdded(added,numMembers,j);
                // if (nextIdx==-1) break;
                if (nextIdx ==-1 || groups[nGroup].happinessLevel + members[nextIdx].happinessLevel < standardLevel){
                    groups[nGroup].members[groups[nGroup].numMembers] = members[j];
                    groups[nGroup].happinessLevel += members[j].happinessLevel;
                    groups[nGroup].numMembers++;
                    added[j] = 1;
                    break;
                }
                else continue;
            }
            else{
                groups[nGroup].happinessLevel += members[j].happinessLevel;
                //Gắn vào cuối mảng nhóm
                groups[nGroup].members[groups[nGroup].numMembers] = members[j];
                groups[nGroup].numMembers++;
                added[j] = 1;
            }

            // if (groups[nGroup].happinessLevel + members[j].happinessLevel >= standardLevel) {
            //     groups[nGroup].members[groups[nGroup].numMembers] = members[j];
            //     groups[nGroup].happinessLevel += members[j].happinessLevel;
            //     groups[nGroup].numMembers++;
            //     i = j; // Move to next member
            //     break;
            // } else {
            //     groups[nGroup].members[groups[nGroup].numMembers] = members[j];
            //     groups[nGroup].happinessLevel += members[j].happinessLevel;
            //     groups[nGroup].numMembers++;
            // }
        }

        // If group happinessLevel is greater than or equal to standardLevel, prepend "Happy " to names
        if (groups[nGroup].happinessLevel >= standardLevel) {
            for (int k = 0; k < groups[nGroup].numMembers; k++) {
                char* newName = malloc(strlen(groups[nGroup].members[k].name) + 6);
                strcpy(newName, "Happy ");
                strcat(newName, groups[nGroup].members[k].name);
                free(groups[nGroup].members[k].name);
                groups[nGroup].members[k].name = newName;
            }
            nGroup++;
        } else {
            free(groups[nGroup].members);
        }
    }

    return groups;
}
Leave a Comment