python学习第一天(单词分析)

发布于:2022-11-01 ⋅ 阅读:(363) ⋅ 点赞:(0)

题目描述

小蓝正在学习一门神奇的语言,这门语言中的单词都是由小写英文字母组 成,有些单词很长,远远超过正常英文单词的长度。小蓝学了很长时间也记不住一些单词,他准备不再完全记忆这些单词,而是根据单词中哪个字母出现得最多来分辨单词。

现在,请你帮助小蓝,给了一个单词后,帮助他找到出现最多的字母和这 个字母出现的次数。

输入描述

输入一行包含一个单词,单词只由小写英文字母组成。

对于所有的评测用例,输入的单词长度不超过 1000。

输出描述

输出两行,第一行包含一个英文字母,表示单词中出现得最多的字母是哪 个。如果有多个字母出现的次数相等,输出字典序最小的那个。

第二行包含一个整数,表示出现得最多的那个字母在单词中出现的次数。

样例

示例 1

输入

lanqiao

输出

a
2

示例 2

输入

longlonglongistoolong

输出

o
6

python代码:

import os
import sys
word = input()
a = 0
b = []
for i in word:
    c = word.count(i)
    if c >= a:
        a = c
for j in word:
    if word.count(j) == a:
        b.append(j)
b.sort()
print(b[0])
print(a)

C语言代码:

#include<stdio.h>
#include<string.h>
int main(){
    char str[1010];
    scanf("%s",str);
    int max = 0;
    char a[1];
    a[0] = str[0];
    for(int i = 0 ;i < strlen(str) ;i++){
            int num = 1;
        for(int j = i + 1; j < strlen(str) ; j++){
            if(str[i] == str[j]){
                num++;
            }
        }
        if(num > max){
            a[0] = str[i];
            max = num;
        }else if(num == max){
            max = num;
            if(str[i] < a[0]){
                a[0] = str[i];
            }
        }
    }
    printf("%c\n",a[0]);
    printf("%d\n",max);
    return 0;
}

知识点:

python中count函数的用法:

https://blog.csdn.net/bnmqwe1/article/details/109534786?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166727032416782425610448%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166727032416782425610448&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-1-109534786-null-null.142^v62^js_top,201^v3^control_1,213^v1^control&utm_term=python%20count%E5%87%BD%E6%95%B0%E7%94%A8%E6%B3%95&spm=1018.2226.3001.4187icon-default.png?t=M85Bhttps://blog.csdn.net/bnmqwe1/article/details/109534786?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166727032416782425610448%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166727032416782425610448&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_click~default-1-109534786-null-null.142^v62^js_top,201^v3^control_1,213^v1^control&utm_term=python%20count%E5%87%BD%E6%95%B0%E7%94%A8%E6%B3%95&spm=1018.2226.3001.4187python中sort函数的用法:

python 的sort()函数详解_qq_20831401的博客-CSDN博客_python sort

 

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到