zoukankan      html  css  js  c++  java
  • PAT 1042 字符统计(20)(思路)

    1042 字符统计(20)(20 分)

    请编写程序,找出一段给定文字中出现最频繁的那个英文字母。

    输入格式:

    输入在一行中给出一个长度不超过1000的字符串。字符串由ASCII码表中任意可见字符及空格组成,至少包含1个英文字母,以回车结束(回车不算在内)。

    输出格式:

    在一行中输出出现频率最高的那个英文字母及其出现次数,其间以空格分隔。如果有并列,则输出按字母序最小的那个字母。统计时不区分大小写,输出小写字母。

    输入样例:

    This is a simple TEST.  There ARE numbers and other symbols 1&2&3...........
    

    输出样例:

    PS:

    e 7

          输入字符串,将每个字符的ASCII码作为下标,若是英文字母,开启计数器,通过max_element找到最大值的地址减去首地址得到下标。最后进行输出。

    #include<iostream>
    #include<string>
    #include<algorithm>
    using namespace std;
    int main()
    {
    	string str;
    	getline(cin, str);
    	int m[128] = { 0 };
    	for (int i = 0; i < str.length(); i++) {
    		if (isalpha(str[i])) {
    			m[tolower(str[i])]++;
    		}
    	}
    	int k = max_element(m, m + 128) - m;
    	cout << char(k) << " " << m[k];
    	return 0;
    }
  • 相关阅读:
    Count_sort C++
    Priority_Queue C++
    HeapSort C++
    Randomized_QuickSort C++
    QuickSort c++
    我的开发环境
    github and eclipse
    centos 7 install eclipse cdt and use github
    github commands
    文化-梁晓声
  • 原文地址:https://www.cnblogs.com/F-itachi/p/9974444.html
Copyright © 2011-2022 走看看