题库027

#include 
#include 
#include 
#include 

// 判断是否为兄弟单词
bool isBrotherWord(char *w1, char *w2)
{
    if (strlen(w1) != strlen(w2) || !strcmp(w1, w2))
    {
        return false;
    }
    int counter[26] = {0};
    for (int i = 0; i < strlen(w1); i++)
    {
        counter[w1[i] - 'a']++;
        counter[w2[i] - 'a']--;
    }
    for (int i = 0; i < 26; i++)
    {
        if (counter[i])
        {
            return false;
        }
    }
    return true;
}

int main()
{
    int n = 0; // 字典单词个数n
    int k = 0; // 第k个兄弟单词
    char strs[1000][11] = {0}; 
    char brothers[1000][11] = {0};
    char strX[11] = {0};

    // 输入单词个数
    scanf("%d", &n);

    // 输入单词
    for (int i = 0; i < n; i++)
    {
        scanf("%s", strs[i]);
    }

    // 输入参照单词
    scanf("%s", strX);

    // k
    scanf("%d", &k);

    int count = 0;
    for (int i = 0; i < n; i++)
    {
        if (isBrotherWord(strX, strs[i]))
        {
            strcpy(brothers[count++], strs[i]);
        }
    }

    // 字典排序,使用冒泡排序法
    for (int i = 0; i < count - 1; i++)
    {
        for (int j = i + 1; j < count; j++)
        {
            if (strcmp(brothers[i], brothers[j]) > 0)
            {
                char tmp[11] = {0};
                strcpy(tmp, brothers[i]);
                strcpy(brothers[i], brothers[j]);
                strcpy(brothers[j], tmp);
            }
        }
    }
    
    printf("%d\n", count);
    puts(brothers[k - 1]);
    

    return 0;
}

你可能感兴趣的:(机考刷题00,算法,数据结构)