0010-统计字符数

问题描述

判断一个由 a-z 这 26 个字符组成的字符串中哪个字符出现的次数最多。

输入

第 1 行是测试数据的组数 n,每组测试数据占 1 行,是一个由 a-z 这 26 个字符组成的字符串,每组测试数据占一行,每行数据不超过 1000 个字符且非空。

输出

输出总共n 行,每行输出对应一个输入。一行输出包括出现次数最多的字符和该字符出现的次数,中间是一个空格。 如果有多个字符出现的次数相同且最多,那么输出 ascii 码最小的那一个字符。

输入样列

2
abbccc
adfadffasdf

输出样例

c 3
f 4

算法实现

using System;

namespace Questions{
    class Program{
        public static void Main(string[] args){
            string input = Console.ReadLine();
            int n = int.Parse(input);
            for (int k = 0; k < n; k++){
                int[] num = new int[26]; 
                input = Console.ReadLine();
                for (int i = 0; i < input.Length; i++) 
                    num[input[i] - 'a']++;
                int maxIndex=0;
                for (int i = 1; i < 26; i++)
                    if (num[i] > num[maxIndex])
                        maxIndex = i; 
                Console.WriteLine("{0} {1}",(char)(maxIndex+'a'),num[maxIndex]);
            }
            Console.ReadKey();
        }
    }
}

你可能感兴趣的:(0010-统计字符数)