首页 > 试题广场 >

字符统计

[编程题]字符统计
  • 热度指数:201846 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入一个只包含小写英文字母和数字的字符串,按照不同字符统计个数由多到少输出统计结果,如果统计的个数相同,则按照ASCII码由小到大排序输出。

数据范围:字符串长度满足


输入描述:

一个只包含小写英文字母和数字的字符串。



输出描述:

一个字符串,为不同字母出现次数的降序表示。若出现次数相同,则按ASCII码的升序输出。

示例1

输入

aaddccdc

输出

cda

说明

样例里,c和d出现3次,a出现2次,但c的ASCII码比d小,所以先输出c,再输出d,最后输出a.
     
#include <stdio.h>
#include <string.h>

int findMax(int Arr1[], int Arr2[]) {
    int max = 0;
    for (int i = 0; i < 10; i++) {
        if (Arr1[i] > max) {
            max = Arr1[i];
        }
    }
    for (int i = 0; i < 26; i++) {
        if (Arr2[i] > max) {
            max = Arr2[i];
        }
    }
    return max;
}

int main() {
    char buffer[1024];
    int cntNumArr[10] = {0};
    int cntCharArr[26] = {0};

    int len;
    scanf("%s", buffer);
    len = strlen(buffer);
    for (int i = 0; i < len; i++) {
        if ((buffer[i] >= '0') && (buffer[i] <= '9')) {
            cntNumArr[buffer[i] - '0'] ++;
        }
        if ((buffer[i] >= 'a') && (buffer[i] <= 'z')) {
            cntCharArr[buffer[i] - 'a'] ++;
        }
    }

    int max;
    while (max = findMax(cntNumArr, cntCharArr)) {
        for (int i = 0; i < 10; i++) {
            if (max == cntNumArr[i]) {
                printf("%d", i);
                cntNumArr[i] = 0;
            }
        }

        for (int i = 0; i < 26; i++) {
            if (max == cntCharArr[i]) {
                printf("%c", 'a'+i);
                cntCharArr[i] = 0;
            }
        }
    }

    return 0;
}

编辑于 2024-03-29 21:31:32 回复(0)
#include <stdio.h>
#include <string.h>

void BubbleSort(char* str)
{
    int len = strlen(str);
    for(int i = 0;i<len-1;i++)
    {
        for(int j = 0;j<len - 1 - i;j++)
        {
            if(str[j] - str[j+1]>0)
            {
                char tmp = str[j];
                str[j] = str[j+1];
                str[j+1] = tmp;
            }
        }
    }
}

int main() {
    char str[1001] = {'\0'};
    char ret[1001] = {'\0'};
    int num[1001] = {0};
    int flag = 0;
    while (scanf("%s",str) != EOF) {
        int len = strlen(str);
        BubbleSort(str);
        int i = 0;
        for (i = 0; i < len - 1;)
        {
            ret[flag] = str[i];
            int j = 0;
            for (j = i + 1; j < len; j++)
            {
                if (str[j] - str[i] != 0)
                {
                    num[flag++] = j - i;
                    i = j;
                    break;
                }
            }
            if (j==len&&i<len-1)
            {
                num[flag++] = j-i;
                break;
            }
        }
        if ( i == len - 1)
        {
            ret[flag] = str[i];
            num[flag++] = 1;
        }
        for(int j = 0;j<strlen(ret)-1;j++)
        {
            for(int k = 0;k<strlen(ret)- 1 - j;k++)
            {
                if(num[k]-num[k+1]<0)
                {
                    int tmpi = num[k];
                    num[k] = num[k+1];
                    num[k+1] = tmpi;
                    char tmpc = ret[k];
                    ret[k] = ret[k+1];
                    ret[k+1] = tmpc;
                }
            }
        }
        printf("%s",ret);
    }
    return 0;
}
发表于 2023-10-11 12:01:08 回复(0)
#include <stdio.h>

int main() {
    char str[1001] = {0};
    gets(str);

    char num[256] = {0};
    int max = 0;
    for (int i = 0; i < strlen(str); i++) {
        num[str[i]]++; //此处已经排序
        if (num[str[i]] > max) {
            max = num[str[i]];
        }
    }
    while (max) {
        for (int i = 0; i < 256; i++) {
            if (num[i] == max) {
                printf("%c", i); //按照出现次数由多到少依次打印
            }
        }
        max--;
    }
    printf("\n");
    return 0;
}
发表于 2023-08-17 14:56:02 回复(0)
/*时间复杂度n级别,不建议采取*/

#include <stdio.h>
#include<string.h>
typedef struct dtr {
    int sum;
    char gg;
} str;
void sort1(str a[], int n) {
    int i, j, tem;
    char op;
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if (a[j].sum > a[i].sum) {
                tem = a[i].sum;
                op = a[i].gg;
                a[i].sum = a[j].sum;
                a[i].gg = a[j].gg;
                a[j].sum = tem;
                a[j].gg = op;
            }

        }
    }
}
void sort2(str a[], int n) {
    int i, j, tem;
    char op;
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if ((a[j].sum == a[i].sum)&&(a[i].gg>a[j].gg)) {
                tem = a[i].sum;
                op = a[i].gg;
                a[i].sum = a[j].sum;
                a[i].gg = a[j].gg;
                a[j].sum = tem;
                a[j].gg = op;
            }

        }
    }
}
void put(str a[], int n) {
    for (int i = 0; i < n; i++) {
        if(a[i].sum)
        printf("%c", a[i].gg);
    }
    printf("\n");
}
int main() {
    char a[1000];
    scanf("%s", a);
    int i, n = strlen(a), ii[36] = {0};
    str dm[26];
    for (i = 0; i < n; i++) {
        if (a[i] >= 'a' && a[i] <= 'z') ii[a[i] - 'a']++;
        else ii[a[i] - '0' + 26]++;
    }
    for (i = 0; i < 36; i++) {
        if (i < 26) {
            dm[i].gg = 'a' + i;
            dm[i].sum = ii[i];
        } else {
            dm[i].gg = '0' + i - 26;
            dm[i].sum = ii[i];
        }
    }
    sort1(dm, 36);
    sort2(dm, 36);
    put(dm, 36);
    return 0;
}
发表于 2023-04-07 23:25:07 回复(0)
#include<stdio.h>

int main(void) {
    char s[1000] = "";

    while(gets(s)) {
        int result[131] = {0};   //Hash
        char s1[1000] = "";      //用于输出
        int maxIndex = 0;
        int max = 0;
        int i = 0;
        int count = 0;
        
        for(i = 0; i < strlen(s); i++) {
            result[s[i]]++;     //result[ ]的下标就是字符的ascii码,,,元素是字符的个数
        }
        for(i = 0; i < 131; i++) {       //统计字符种数,下面的for( )就不用执行到131了,节省了(131-count)*131的复杂度       (是时间复杂还是空间复杂呢?????)
            if(result[i] > 0) 
                count++;
        }
        for(int j =0;j < count;j++) {    //写j<131不够好
            max = 0;
            maxIndex = 0;
            for(i = 0; i < 131; i++) {
                if(max < result[i]) {
                    max = result[i];          //for()结束时,max是出现次数最多的字符的个数
                    maxIndex = i;            //for()结束时,maxIndex是出现次数最多的字符的下标
                }
            }
            result[maxIndex] = 0;        //过河拆桥,置字符的个数为0
            s1[j] = (char)maxIndex;      //将字符存档,准备输出
            
        }
        printf("%s\n", s1);
    }
}

发表于 2022-07-24 20:46:12 回复(2)
#include <stdio.h>
#define    N    36
#define    M    1000
int find_max(int arr[])
{
    int i,max=0;
    for(i=1;i<N;i++)
    {
        if(arr[max]<arr[i])
            max=i;
    }
    if(arr[max]>0)
    {
        arr[max]=0;
        return max; 
    }
    else
        return -1;
}
int main()
{
    int arr[N]={0},i=0,max;
    char str[M];
    scanf("%s",str);
    while(str[i]!='\0')
    {
        if(str[i]>='0'&&str[i]<='9')
            arr[str[i]-'0']++;
        else
            arr[str[i]-'a'+10]++;
        i++;
    }
    for(i=0;i<N;i++)
    {
        max=find_max(arr);
        if(max>=0&&max<=9)
            printf("%c",max+'0');
        else if(max>=10&&max<=35)
            printf("%c",max-10+'a');
    }
    return 0;
}

发表于 2022-04-26 17:49:16 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char str[1001];
    scanf("%s", str);
    int len = strlen(str);
    int hash[128] = {0};
    int max = 0;

    for (int i = 0; i < len; ++i) {
        hash[str[i]]++; //统计每个字符出现的次数
        if (hash[str[i]] > max) {
            max = hash[str[i]];//找到最大的次数
        }
    }

    for (int i = max; i > 0; --i) { //找到最大次数的字符
        for (int j = 0; j < 128; ++j) {
            if (hash[j] == i) {
                printf("%c", j);
            }
        }
    }
    return 0;
}
发表于 2022-04-19 20:57:03 回复(1)
C 一直嵌套 觉得不错点个赞 #define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
int main()
{
    char arr[1001]={0};
    int cnt[128]={0};
    int snt[128]={0};
    int i,j;
    scanf("%s",arr);
    int len=strlen(arr);
    for(i=0;i<len;i++)
    {
        cnt[arr[i]]++;
    }
    for(i=0;i<128;i++)
    {
        if(cnt[i]!=0)
            snt[cnt[i]]=1;
    }
    for(i=len;i>0;i--)
    {
        if(snt[i]==1)
        {
            for(j='0';j<='z';j++)
            {
                if(cnt[j]==i)
                    printf("%c",j);
            }
        }
    }
    return 0;
}

发表于 2022-03-23 01:30:26 回复(0)
比较直观的方法
/*
思路:用一个表count记录次数,项目按ascii顺序,前10个记录数字次数,后26个记录小写字母
     再用一个表ch存放0-9和a-z,与count位置一一对应
     然后对count和ch表一起采用稳定的排序方法降序排序
     最后只输出ch表中count不为0的部分
复杂度:冒泡排序时间复杂度O(n^2); 只有数字和小写字母的不变长度表,空间复杂度O(1)
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void bubbleSort(int count[36], char ch[36])
{
    int temp;
    int flag = 1;
    for(int i=0; i<35-1; i++){
        flag = 1;
        for(int j=35; j>i; j--){
            if( count[j] > count[j-1] ){
                temp       = count[j];
                count[j]   = count[j-1];
                count[j-1] = temp;
                
                temp       = ch[j];
                ch[j]      = ch[j-1];
                ch[j-1]    = temp;
                
                flag = 0;
            }
        }
        if(flag){
            break;
        }
    }
}

int main(void)
{
    char in[1000];
    while(scanf("%s", in) != EOF){
        int count[36] = {0};
        char ch[36] = "0123456789abcdefghijklmnopqrstuvwxyz";
        
        for(int i=0; i<strlen(in); i++){
            if( isdigit(in[i]) ){
                count[ in[i]-'0' ]++;
            }else if( isalpha(in[i]) ){
                count[ in[i]-'a'+10 ]++;
            }
        }
        
        bubbleSort(count, ch);
        
        for(int i=0; i<36; i++){
            if(count[i] > 0){
                printf("%c", ch[i]);
            }
        }
        putchar('\n');
    }
    
    return 0;
}


发表于 2022-01-23 21:05:09 回复(0)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Size 1000

typedef struct table{
    char *elem;
    int *count;
}table;

void swapch(char *a,char *b){
    char t=*a;
    *a=*b;
    *b=t;
}

void swapint(int *a,int *b){
    int t=*a;
    *a=*b;
    *b=t;
}

int main()
{
    char str[Size]={'\0'};
    while(scanf("%[^\n]",str)!=EOF){
        scanf("%*[^\n]");scanf("%*c");
        
        table t;
        int len=36;
        t.elem=(char*)malloc(len*sizeof(char));
        t.count=(int*)malloc(len*sizeof(int));
        
        for(int i=0;i<10;i++){
            t.elem[i]=i+48;
            t.count[i]=0;
        }
        for(int i=10;i<len;i++){
            t.elem[i]=i+87;
            t.count[i]=0;
        }
        
        for(int i=0;i<len;i++){
            for(int j=0;j<strlen(str);j++){
                if(t.elem[i]==str[j]){
                    t.count[i]++;
                }
            }
        }
        
        for(int i=0;i<len;i++){
            for(int j=i+1;j<len;j++){
                if(t.count[i]<t.count[j]){
                    swapch(&t.elem[i],&t.elem[j]);
                    swapint(&t.count[i],&t.count[j]);
                }
            }
        }

        for(int i=0;i<len;i++){
            for(int j=i+1;j<len;j++){
                if(t.count[i]==t.count[j]){
                    if(t.elem[i]>t.elem[j]){
                        swapch(&t.elem[i],&t.elem[j]);
                        swapint(&t.count[i],&t.count[j]);
                    }
                }
            }
        }
        
        
        for(int i=0;i<len;i++){
            if(t.count[i]!=0){
                printf("%c",t.elem[i]);
            }
        }
        printf("\n");
        
        free(t.elem);
        free(t.count);
        
        for(int i=0;i<strlen(str);i++){
            str[i]='\0';
        }

    }
    return 0;
}




发表于 2022-01-22 18:11:43 回复(0)
#include<stdio.h>
int main(){
    char str[2048]={0};
    int map[128];
    int sum[100];
    while(fgets(str,sizeof(str),stdin)){
        memset(map,0,sizeof(map));
        memset(sum,0,sizeof(sum));
        int len = strlen(str);
        for(int i=0;i<len;i++){
           map[(int)str[i]]++;
        }
        //fwrite(str,len, 1,stdout);
        int j=0;
        for(int i=47;i<128;i++){
             if(map[i]!=0){
                 sum[j]=map[i];
                 j++;
                                  
             }
        }
  // 排序
        for(int k=0;k<j;k++){
            for(int m=0;m<j-1-k;m++){
                if(sum[m] <= sum[m+1]){
                    int temp = sum[m];
                    sum[m] = sum[m+1];
                    sum[m+1] = temp;
                }
            }
        }
        //debug
     //for(int k=0;k<j;k++){  printf("mum[%d]=%d ",k,sum[k]);}
 //打印
    for(int n=0;n<j;n++){
        while(sum[n+1]==sum[n]){
            n++;
        }
       for(int i=47;i<128;i++){
             if(map[i]==sum[n]){
                 printf("%c",i);
             }
        }
    }
     printf("\n");
    }
    return 0;
}
发表于 2021-12-29 13:27:17 回复(1)
// hash + qsort排序
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct {
    char c;
    int count;
} HashMap;

HashMap g_hashMap[36] = {0};
int g_index = 0;

int GetIndex(char c)
{
    for (int i = 0; i < sizeof(g_hashMap) / sizeof(HashMap); i++) {
        if (g_hashMap[i].c == c) {
            return i;
        }
    }
    
    return -1;
}

void InsertHashMap(char c)
{
    int i = GetIndex(c);
    if (i >= 0) {
        g_hashMap[i].count++;
        return;
    }
    g_hashMap[g_index].c = c;
    g_hashMap[g_index].count = 1;
    g_index++;
}

int Cmp(const void *p1, const void *p2)
{
    HashMap *a = (HashMap *)p1;
    HashMap *b = (HashMap *)p2;
    
    if (a->count != b->count) {
        return b->count - a->count;
    } else {
        return a->c - b->c;
    }
}

int main(void)
{
    char str[1024] = {'\0'};
    
    while (gets(str)) {
        memset(g_hashMap, 0, sizeof(g_hashMap));
        g_index = 0;
        for (int i = 0; i < strlen(str); i++) {
            InsertHashMap(str[i]);
        }
        qsort(g_hashMap, g_index, sizeof(HashMap), Cmp);
        
        for (int i = 0; i < g_index; i++) {
            printf("%c", g_hashMap[i].c);
        }
        
        printf("\n");
    }
}

发表于 2021-09-20 16:11:59 回复(0)