对二维字符数组中的列进行排序

发布于 2024-12-29 19:28:32 字数 1035 浏览 1 评论 0原文

我有一个二维数组,其中顶行是一行字母(字母表),底行是上面的字母在字符串中出现的频率。这个想法是将字母按照出现频率的顺序排列在顶行。

目前:

输入:

quickbrownfoxjumpsoverthelazydog

输出:

abcdefghijklmnopqrstuvwxyz

11112111111111411211211111

所需输出:

oeruabcdfghijklmnpqstvwxyz

42221111111111111111111111

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

main()
{
    char string[100];
    char s[26][2];
    int c = 0, count[26] = {0};

    printf("Enter a string\n");
    gets(string);

    while ( string[c] != '\0' )
    {
        if ( string[c] >= 'a' && string[c] <= 'z' ) 
        count[string[c]-'a']++;

        c++;
    }


    for ( c = 0 ; c < 26 ; c++ )
    {
        if( count[c] != 0 )
        {
            s[c][1]=c+'a';
            s[c][2]= (char)(((int)'0')+count[c]);
            gotoxy(c,1);
            printf("%c",s[c][1]);
            gotoxy(c,2);
            printf("%c",s[c][2]);
        }
    }

    return 0;
}

I have a 2D array where the top row is a row of letters (the alphabet) and the bottom row is the frequency that the letter above it occurs in a string. The idea is to place the letters in order of frequency along the top row.

At the moment:

input:

quickbrownfoxjumpsoverthelazydog

output:

abcdefghijklmnopqrstuvwxyz

11112111111111411211211111

Desired output:

oeruabcdfghijklmnpqstvwxyz

42221111111111111111111111

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>

main()
{
    char string[100];
    char s[26][2];
    int c = 0, count[26] = {0};

    printf("Enter a string\n");
    gets(string);

    while ( string[c] != '\0' )
    {
        if ( string[c] >= 'a' && string[c] <= 'z' ) 
        count[string[c]-'a']++;

        c++;
    }


    for ( c = 0 ; c < 26 ; c++ )
    {
        if( count[c] != 0 )
        {
            s[c][1]=c+'a';
            s[c][2]= (char)(((int)'0')+count[c]);
            gotoxy(c,1);
            printf("%c",s[c][1]);
            gotoxy(c,2);
            printf("%c",s[c][2]);
        }
    }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往事随风而去 2025-01-05 19:28:32

这是一种方法:每次计数后,将其左移五位,然后按位或低五位中的字母数。对结果数字进行排序,并通过与 0x1F 进行“与”运算并添加 'a' 将其最低有效五位转换回字母。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int compare (const void * a, const void * b) {
    int ia = *(int*)a;
    int ib = *(int*)b;
    return (ia>>5) == (ib>>5) ? ia-ib : ib-ia;
}

int main(int argc, char* argv[]) {
    char string[100] = "quickbrownfoxjumpsoverthelazydog";
    int c = 0, count[26];
    memset(count, 0, sizeof(count));
    while ( string[c] != '\0' ) {
        if ( string[c] >= 'a' && string[c] <= 'z' ) {
            count[string[c]-'a']++;
        }
        c++;
    }
            // Shift the letter ordinal into the lower five bits of the count*32
    for ( c = 0 ; c < 26 ; c++ ) {
        count[c] = (count[c] << 5) | c;
    }
            // Sort the results using the custom compare function
    qsort(count, 26, sizeof(int), compare);
            // Print the letters by taking the lower five bits
    for ( c = 0 ; c < 26 ; c++ ) {
        printf("%c", 'a'+(count[c]&0x1F));
    }
    printf("\n");
            // Print the counts by ignoring the lower five bits
    for ( c = 0 ; c < 26 ; c++ ) {
        printf("%d", count[c] >> 5);
    }
    return 0;
}

Here is one approach: take each count, shift it left by five, and bitwise-or the number of the letter in the lower five bits. Sort the resulting numbers, and convert their least-significant five bits back to the letter by and-ing with 0x1F and adding 'a'.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int compare (const void * a, const void * b) {
    int ia = *(int*)a;
    int ib = *(int*)b;
    return (ia>>5) == (ib>>5) ? ia-ib : ib-ia;
}

int main(int argc, char* argv[]) {
    char string[100] = "quickbrownfoxjumpsoverthelazydog";
    int c = 0, count[26];
    memset(count, 0, sizeof(count));
    while ( string[c] != '\0' ) {
        if ( string[c] >= 'a' && string[c] <= 'z' ) {
            count[string[c]-'a']++;
        }
        c++;
    }
            // Shift the letter ordinal into the lower five bits of the count*32
    for ( c = 0 ; c < 26 ; c++ ) {
        count[c] = (count[c] << 5) | c;
    }
            // Sort the results using the custom compare function
    qsort(count, 26, sizeof(int), compare);
            // Print the letters by taking the lower five bits
    for ( c = 0 ; c < 26 ; c++ ) {
        printf("%c", 'a'+(count[c]&0x1F));
    }
    printf("\n");
            // Print the counts by ignoring the lower five bits
    for ( c = 0 ; c < 26 ; c++ ) {
        printf("%d", count[c] >> 5);
    }
    return 0;
}
棒棒糖 2025-01-05 19:28:32

有几点:

  1. C 中的数组是零源的,也就是说您的索引应该是 s[c][0]s[c][1] - 你的代码没有崩溃,因为你很幸运。
  2. 通常,数组会以相反的方式排序:c[2][26]

至于排序……因为我们只处理 26 个元素,而不是 260 万个元素,那么如何以及何时排序这没什么影响。你可以通过冒泡排序来逃脱。您可以花哨地构建某种双向链接列表,然后对其进行排序,并重新排列元素。玩得开心!

A couple of things:

  1. Arrays in C are zero-origin, which is to say that your indexes should be s[c][0] and s[c][1] - your code isn't crashing b/c you're lucky.
  2. Typically, the array would be ordered the other way around: c[2][26]

As to the sorting … since we're only working with 26 elements and not 2.6 million, how and when you do it is of little consequence. You can get away with doing a bubble sort. You could get fancy and build some sort of doubly-linked list and sort it as you go, shuffling elements around. Have fun with it!

十年不长 2025-01-05 19:28:32

您应该找到每次迭代的最大值。你可以使用一些快速排序算法,但一个简单的解决方案是:(

for (c=0;c<26;c++) {
 int max=-1,letter,i;
 for (i=0;i<26;i++)
  if (count[i]>max) {
   max=count[i];
   letter=i;
  }
 count[letter]=-1;
 gotoxy(c,1);
 s[c][0]='a'+letter;
 printf("%c",s[c][0]);
 gotoxy(c,2);
 s[c][1]='0'+max;
 printf("%c",s[c][1]);
}

当然,假设max<10)

You should find the maximum every iteraton. You can use some fast sort algorithm, but a simple solution is:

for (c=0;c<26;c++) {
 int max=-1,letter,i;
 for (i=0;i<26;i++)
  if (count[i]>max) {
   max=count[i];
   letter=i;
  }
 count[letter]=-1;
 gotoxy(c,1);
 s[c][0]='a'+letter;
 printf("%c",s[c][0]);
 gotoxy(c,2);
 s[c][1]='0'+max;
 printf("%c",s[c][1]);
}

(Of course, assuming that max<10)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文