【NYOJ】[243]字母统计

这里写图片描述

今天大脑不在状态
就随便水水好了……

用一个数组来记录字母数
然后来寻找次数最多的那一个下标

#include<stdio.h>
#include<string.h>
int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int a[26];
        memset(a,0,sizeof(a));
        char s[1020];
        scanf("%s",s);
        for(int i=0; i<strlen(s); i++) {
            a[s[i]-'a']++;
        }
        int maxi=0;
        for(int i=0; i<26; i++) {
            if(a[i]>a[maxi])
                maxi=i;
        }
        printf("%c\n",maxi+'a');
    }
    return 0;
}

标程的思路一样~

#include<stdio.h>
#include<string.h>
main() {
    int x,i,max,q;
    char a[1011];
    scanf("%d",&x);
    getchar();
    while(x--) {
        int s[26]= {0};
        gets(a);
        for(i=strlen(a)-1; i>=0; i--)
            s[a[i]-97]++;
        max=0;
        for(i=0; i<26; i++)
            if(max<s[i]) max=s[i],q=i;
        printf("%c\n",q+97);
    }
}

题目地址:【NYOJ】[243]字母统计

原文地址:https://www.cnblogs.com/BoilTask/p/12569826.html