20150912华为机考1之"输入一个字符串,将其中出现次数最多的字符输出"

不吐槽华为的服务器了,直接上正文

输入:字符串(英文字母),长度不超过128

输出:出现频率最高的字母

思路写在注释文档

 

/*  Input a string
 *  Output the most frequent character
 *
 *  The way of thinking:
 *  using ASCII, count the number of each character
 *  then find out the max number(max_num)
 *  and its according index(max_index)
 *  so the most frequent character is (index + 'a')
 *
 *  time_complexity:O(n)?
 */
#include <stdio.h>
#include <string.h>

int main(){
    char s[128]={0};
    int count[26]={0};
    int len=0, i=0;
    int max_num, max_index;
    
    scanf("%s", s);
    len = (int)strlen(s);
    for(i=0; i<len; i++)
        count[(s[i]-'a')] += 1;
    
    max_num=count[0];
    max_index=0;
    for(i=1; i<26; i++){
        if(count[i] > max_num){
            max_num = count[i];
            max_index = i;
        }
    }
    
    printf("%c\n%d\n", (max_index+'a'), count[max_index]);
    
    return 0;
}

 

转载于:https://www.cnblogs.com/Cmfvacks-IsLjj/p/4805555.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值