POJ 2136 Vertical Histogram

↘锁芯ラ 提交于 2020-04-07 07:45:45

Vertical Histogram

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20245   Accepted: 9626

Description

    Write a program to read four lines of upper case (i.e., all CAPITAL LETTERS) text input (no more than 72 characters per line) from the input file and print a vertical histogram that shows how many times each letter (but not blanks, digits, or punctuation) appears in the all-upper-case input. Format your output exactly as shown.

Input

* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.

Output

* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines. 

Sample Input

THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
THIS IS AN EXAMPLE TO TEST FOR YOUR
HISTOGRAM PROGRAM.
HELLO!

Sample Output

*
                            *
        *                   *
        *                   *     *   *
        *                   *     *   *
*       *     *             *     *   *
*       *     * *     * *   *     * * *
*       *   * * *     * *   * *   * * * *
*     * * * * * *     * * * * *   * * * *     * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

题意:统计输入的四行字符串中每个大写字母的个数,并按给定样例的格式输出。

Solution:

    有处小坑,如果用map记录每个字母出现的次数,必须要保证每个字母的次数初始化为0,或者有防止空指针的操作,否则后出现Runtime Error, 比较烦;用数组就可以完美避开这个问题。。

    结果是逐行输出的,每次将max减1,如果max的值和某个字母出现的次数相同,就输出‘*’,否则空格

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (char c = 'A'; c <= 'Z'; c++) {
            map.put(c, 0);
        }
        for (int i = 0; i < 4; i++) {
            String s = br.readLine();
            for (char c : s.toCharArray()) {
                if (c >= 'A' && c <= 'Z') {
                    if (map.containsKey(c)) {
                        map.put(c, map.get(c) + 1);
                    } else {
                        map.put(c, 1);
                    }
                }
            }
        }
        br.close();

        int maxLen = 0;
        for (Map.Entry<Character, Integer> me : map.entrySet()) {
            maxLen = Math.max(me.getValue(), maxLen);
        }

        for (int j = maxLen; j >= 1; j--) {
            for (int i = 0; i < 26; i++) {
                if (map.get((char)(i + 'A')) < j) {
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }
                if (i < 25) {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
        for (char c = 'A'; c < 'Z'; c++) {
            System.out.print(c + " ");
        }
        System.out.print('Z');
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!