zoukankan      html  css  js  c++  java
  • java 统计英文字符

    import java.util.Arrays;
    
    /*
     * The Associated Press won an award for its series on 
    the profiling of Muslims by the New York Police 
    Department.
    But for the first time since 1977 there was no prize 
    for fiction.The Pulitzer panel praised the Patriot-News.
    统计出英文字符的个数,打印出出现的次数及字符
    打印出出现次数最多的字符及次数
     */
    public class temp {
        public static void main(String[] args) {
            String str = "The Associated Press won an award for its series on"
                    +"the profiling of Muslims by the New York Police Department."
                    +"But for the first time since 1977 there was no prize"
                    +"for fiction.The Pulitzer panel praised the Patriot-News.";
            //定义数组存储26个字母
            char[] arr = new char[26];
            int index = 0;
            for(char chars = 'a'; chars <= 'z'; chars++){
                arr[index] = chars;
                index++; 
            }
            System.out.println(Arrays.toString(arr));
            //将字符串中的字母变成小写
            str = str.toLowerCase();
            //定义一个数组存储每个字符出现的次数
            int[] nArr = new int[26];
            for(int i = 0;i < arr.length;i++){
                for(int j = 0;j < str.length();j++){
                    if(str.charAt(j) == arr[i]){
                        nArr[i]++;
                    }
                }
            }
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i] + "=" +nArr[i]);
            }
            //找出nArr数组中的最大值
            int max = 0;
            for (int i = 0; i < nArr.length; i++) {
                if(nArr[i] > max){
                    max = nArr[i];
                }
            }
            System.out.println("输出出先次数最多的元素及次数");
            for (int i = 0; i < nArr.length; i++) {
                if(nArr[i] == max){
                    System.out.println(arr[i] + "=" + nArr[i]);
                }
            }
        }
    }
  • 相关阅读:
    三分法
    string常用函数的整理
    一句话 讲解 kmp的 next 数组 看不懂的 直接来掐死我吧
    http://www.codeforces.com/contest/703/problem/D D. Mishka and Interesting sum (莫队的TLE)
    Codeforces Round #365 (Div. 2) C
    数论
    默慈金数
    转载:HTTP 请求头中的 X-Forwarded-For
    Glusterfs volume 的三种挂载方式
    GlusterFS 配置及使用
  • 原文地址:https://www.cnblogs.com/wwjdx/p/6443975.html
Copyright © 2011-2022 走看看