Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
12 kB
8
Indexable
Never
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

void makeHappiness(char input[]){
    printf("Happy %s",input);
}


int is_vowel(char c) {
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'||
           c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
}
void calculateHappinessScore(char input[]){
    // khởi tạo 1 mảng int 256 phần tử, mỗi phần tử đều có giá trị 0
    int score = 0;
    for(int i = 0; input[i] != '\0'; i++){
        score += is_vowel(input[i])?(int)input[i]:0;
    }
    printf("Happiness Score: %d",score);
    if(score > 100){
        printf(" (Happy)\n");
    }
    else {
        printf(" (Unhappy)\n");
    }
}


int isPrime(int n){
    for(int i = 2; i <= sqrt(n); i++){
        if(n%i == 0){
            return 0;
        }
    }
    return n>1;
}

//kiểm tra 1 số có phải là lũy thừa của cơ số 2 hay không
    int isPowerOfTwo(int n){
        return n>0 && (n & (n-1)) == 0;
    }

//kiểm tra 1 có tổng các chữ số là lũy thừa của 2 hay không
int is_happy_number(int n){
    int sum = 0;
    while(n > 0){
        sum += n%10;
        n /= 10;
    }
    return isPowerOfTwo(sum);
}

//Hàm chuyển chuỗi thành số nguyên
int stringToInt(char input[]){
    int n = 0;
    for(int i = 0; input[i] != '\0'; i++){
        n = n*10 + (input[i] - '0');
    }
    return n;
}

int sumOfDigits(int n){
    int sum = 0;
    while(n > 0){
        sum += n%10;
        n /= 10;
    }
    return sum;
}

int findHappinestNumber(char input[]) {
    // Calculate the length of input string and allocate buffer dynamically
    int len = strlen(input);
    char *buffer = (char*)malloc((len + 1) * sizeof(char));
    if(len==0) return 0;

    //Fix (maybe)
    //initial
    // strncpy(buffer, input, len + 1);
    // fixed
    strcpy(buffer, input);
    buffer[len] = '\0';

    char *pch;
    pch = strtok(buffer, ",");
    int result = 0;

    while (pch != NULL) {
        int n = stringToInt(pch);
        if (isPrime(n) && is_happy_number(n)) {

            //Whether > or >= ???
            if (sumOfDigits(n) > sumOfDigits(result)) {
                result = n;
            }
        }
        pch = strtok(NULL, ",");
    }

    return result;
}

//đếm số lượng nguyên âm khác nhau
int countVowels(char input[]){
    int count = 0;
    int vowels[256] = {0};
    for(int i = 0; input[i] != '\0'; i++){
        if(is_vowel(input[i]) && vowels[input[i]] == 0){
            count++;
            vowels[input[i]] = 1;
        }
    }
    return count;
}

void find_happiest_string(char *input, char *result, int* max_score) {
    int len = strlen(input);
    *max_score = 0;
    char *temp = (char*)malloc((strlen(input) + 1)*sizeof(char));;

    for (int i = 0; i < len; i++) {
        if (is_vowel(input[i])) {
            int score = 0;
            int temp_len = 0;
            //i love you
            for (int j = i; j < len; j++) {
                if (is_vowel(input[j])) {
                    score += input[j];
                    temp[temp_len++] = input[j];
                }
               
            }
            temp[temp_len] = '\0';
            printf("temp in i: %s\n",temp);

            if (is_happy_number(score) && score > *max_score) {
                *max_score = score;
                strcpy(result, temp);
            } else if (is_happy_number(score) && score == *max_score) {
                if (strlen(temp) < strlen(result) && countVowels(temp) > countVowels(result)) {
                    strcpy(result, temp);
                } else if (strlen(temp) == strlen(result) && 
                countVowels(temp) == countVowels(result) && 
                strcmp(temp, result) > 0) {
                    strcpy(result, temp);
                }
            }
        }
    }
}

void to_hop(char *chuoi, char *hien_tai, int index, char ket_qua[][MAX_LENGTH], int *count) {
    // Thêm tổ hợp hiện tại vào mảng kết quả
    strcpy(ket_qua[(*count)++], hien_tai);

    // Lặp qua tất cả các ký tự còn lại trong chuỗi
    for (int i = index; i < strlen(chuoi); ++i) {
        hien_tai[strlen(hien_tai)] = chuoi[i];
        to_hop(chuoi, hien_tai, i + 1, ket_qua, count);
        hien_tai[strlen(hien_tai) - 1] = '\0'; // Loại bỏ ký tự cuối cùng để thử tổ hợp mới
    }
}

void maximizeHappiestScore(char input[]) {
    char *happiest_string = (char*)malloc((strlen(input) + 1)*sizeof(char));
    int max_score = 0;
    int count = 0;
    char *hien_tai = (char*)malloc((strlen(input) + 1)*sizeof(char));
    char **ket_qua = (char**)malloc(pow(2,strlen(input))*sizeof(char*)); 
    for(int i = 0; i < pow(2,strlen(input)); i++){
        ket_qua[i] = (char*)malloc((strlen(input) + 1)*sizeof(char));
    }
    to_hop(input, hien_tai, 0, ket_qua, &count);

    if (max_score > 100) {
        printf("Happiest String: %s\nHappiest Score: %d (Happy)\n", happiest_string, max_score);
        } else {
        printf("Happiest String: %s\nHappiest Score: %d (Unhappy)\n", happiest_string, max_score);
    }
}


typedef struct {
    char* name;
    int happinessLevel;
} Member;

typedef struct {
    Member* members;
    int numMembers;
    int happinessLevel;
} Group;

int compareMembers(const void* a, const void* b) {
    return ((Member*)b)->happinessLevel - ((Member*)a)->happinessLevel;
}

int getNextMemberNotAdded(int *added, int numMembers, int start){
    for(int i = start+1; i < numMembers; i++){
        if(added[i] == 0){
            return i;
        }
    }
    return -1;
}

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;
}

// void printGroups(Group* groups, int numGroups) {
//     for (int i = 0; i < numGroups; i++) {
//         printf("Group %d: [", i);
//         for (int j = 0; j < groups[i].numMembers; j++) {
//             if (j > 0) printf(", ");
//             printf("{\"%s\", %d}", groups[i].members[j].name, groups[i].members[j].happinessLevel);
//         }
//         printf("]\n");
//     }
// }

// int main() {
//     Member members[] = {
//         {"Alice", 5},
//         {"Bob", 7},
//         {"Charlie", 3},
//         {"David", 8},
//         {"Eve", 6},
//         {"Frank", 4},
//         {"Grace", 9},
//         {"Henry", 2},
//         {"Ivan", 1}
//     };

//     int numMembers = sizeof(members) / sizeof(members[0]);
//     int standardLevel = 6;
//     int numGroups = 0;

//     Group* groups = splitGroups(members, numMembers, standardLevel, &numGroups);

//     printGroups(groups, numGroups);

//     // Free allocated memory
//     for (int i = 0; i < numGroups; i++) {
//         for (int j = 0; j < groups[i].numMembers; j++) {
//             free(groups[i].members[j].name);
//         }
//         free(groups[i].members);
//     }
//     free(groups);

//     return 0;
// }


int main(){
    // makeHappiness("Birthday!");
    // calculateHappinessScore("Hello, World!");
    // calculateHappinessScore("Sad");
    // printf("Prime: %d\n",isPrime(12)); 
    // printf("Power of 2: %d\n",isPowerOfTwo(13));
    // char input[] = "13,89,53,45,67";
    // printf("%d",findHappinestNumber(input));
    char input[11] = "i love you";
    maximizeHappiestScore(input);
    // printf("%d", findHappinestNumber("12,88,4,45,62"));
    //    Member members[] = {
    //     {"Alice", 5},
    //     {"Bob", 7},
    //     {"Charlie", 3},
    //     {"David", 8},
    //     {"Eve", 6},
    //     {"Frank", 4},
    //     {"Grace", 9},
    //     {"Henry", 2},
    // };

    // int standardHappinessLevel = 12;

    // Group* groups = splitGroups(members, 8, standardHappinessLevel);

    // for (int i = 0; i < 3; i++) {
    //     printf("Group %d (Happiness level: %d): ", i + 1, groups[i].happinessLevel);
    //     for (int j = 0; j < groups[i].numMembers; j++) {
    //         printf("%s ", groups[i].members[j].name);
    //     }
    //     printf("\n");
    // }
    // free(groups);
    return 0;
}
// Group 1 (Happiness level: 12): Happy Charlie Happy Grace
// Group 2 (Happiness level: 12): Happy David Happy Frank
// Group 3 (Happiness level: 12): Happy Alice Happy Bob
Leave a Comment