首页 新闻 会员 周边

C#求单词出现最多的字母及次数

0
悬赏园豆:50 [已解决问题] 解决于 2021-05-30 13:35

用C#编写以下问题:
【输入格式】 输入一行包含一个单词,单词只由小写英文字母组成。
【输出格式】 输出两行,第一行包含一个英文字母,表示单词中出现得最多的字母是哪个。如果有多个字母出现的次数相等,输出字典序最小的那个。第二行包含一个整数,表示出现得最多的那个字母在单词中出现的次数。

例如输入:
Lanqiao

输出:
a
2

臭宝要努力的主页 臭宝要努力 | 初学一级 | 园豆:53
提问于:2021-05-28 00:48
< >
分享
最佳答案
0

static void Main(string[] args)
{
var str = Console.ReadLine();
var re = GetMaxCountChar(str);
Console.WriteLine(re.Item1);
Console.WriteLine(re.Item2);
}
static Tuple<char, int> GetMaxCountChar(string str)
{
//func 1 linq
var r = str.GroupBy(g => g)
.Select(g => Tuple.Create(g.Key, g.Count()));
var maxCount = r.Max(g => g.Item2);
var c = r.Where(g => g.Item2 == maxCount).Min(g => g.Item1);
return Tuple.Create(c, maxCount);

        //func 2 hash + linq
        var dic = new Dictionary<char, int>();
        foreach (var item in str)
        {
            if (dic.ContainsKey(item))
            {
                dic[item] = dic[item] + 1;
            }
            else
            {
                dic[item] = 1;
            }
        }
        var count = dic.Values.Max();
        var c = dic.Where(item => item.Value == count)
            .Min(item => item.Key);
        return Tuple.Create(c, count);
    }
收获园豆:25
彭小立 | 小虾三级 |园豆:634 | 2021-05-28 09:29
其他回答(1)
0
using System;
using System.Linq;
using System.Collections.Generic;

namespace CharCount
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = null;
            char letter = '0';
            int count = 0;

            Console.WriteLine("please input a word or input '!q' to quit");

            while (true)
            {
                input = Console.ReadLine().Trim();
                if (string.IsNullOrWhiteSpace(input))
                {
                    continue;
                }
                if (input == "!q")
                {
                    Console.WriteLine("bye");
                    break;
                }
                (letter, count) = GetMaximumLetter(input);
                Console.WriteLine($"{letter}\r\n{count}");
            }
        }

        private static (char letter, int count) GetMaximumLetter(string input)
        {
            Dictionary<char, int> dic = new Dictionary<char, int>();

            foreach (var item in input)
            {
                if (dic.Keys.Contains(item))
                {
                    dic[item] += 1;
                }
                else
                {
                    dic.Add(item, 1);
                }
            }

            return GetMaximumLetterFromDic(dic);
        }

        private static (char letter, int count) GetMaximumLetterFromDic(Dictionary<char, int> dic)
        {
            char maximumKey = '0';
            int count = 0;

            foreach (var key in dic.Keys)
            {
                if (dic[key] > count)
                {
                    maximumKey = key;
                    count = dic[key];
                }
            }

            return (maximumKey, count);
        }
    }
}

别忘了结贴。

收获园豆:25
会长 | 园豆:12401 (专家六级) | 2021-05-28 09:45
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册