一、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

二、题解

目标:计算出给定的4行字符序列中每个字母出现的次数用“ * ”表示,并绘制出直方图。

问题:1、计算字母出现的次数。 2、打印直方图

方法:1、依次读入每一行到一个字符串,将26个字母依次和字符串中的字符比较,相符则加1.把结果存放到大小为26的数组a中。

2、找出a数组中的最大数max,将max循环递减,每次循环比较a数组和max,如果相等则输出“* ”,否则输出空格。每次比较完一个max,换行一次。最后输出A~Z即    可。

注意:难点在于输出,刚开始的时候也没明白。后来一想也挺简单的。刚开始的时候就用SC.next()读入四个数组,结果怎么都不对,后来发现犯傻了。应该是读入一行才对,呵呵。这个老师不记得, BufferedReader br=new BufferedReader(new InputStreamReader(System.in));,提醒自己一下。

三、java代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class Main {
static int[] a=new int [27];
static int max=-1;
public static void count(String s){
int i;
for(i=0;i<s.length();i++){
for(int k=65;k<=90;k++){
if((int)s.charAt(i)==k){
a[k-64]++;
}
if(a[k-64]>max)
max=a[k-64];
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] s=new String[4];
int i,j;
char c;
for(i=0;i<4;i++){
s[i]=br.readLine();
count(s[i]);
}
for(j=max;j>=1;j--){
for(i=1;i<=26;i++){
if(a[i]<j)
System.out.print(" ");
else
System.out.print("* ");
}
System.out.println();
}
for(c='A';c<='Z';c++){
System.out.print(c+" ");
}
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Poj 2136 Vertical Histogram(打印垂直直方图)的更多相关文章

  1. POJ 2136 Vertical Histogram(当时写的比较恶心,优化一下)

    Vertical Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21223 Accepted: 10048 ...

  2. poj 2136 Vertical Histogram 解题报告

    题目链接:http://poj.org/problem?id=2136 题意不难理解,就是输入四行字符串(每行字符总数不超过72个),统计26个英文字母的数目,并按柱状图的形式输出.我的思路就是,先用 ...

  3. POJ 2136 Vertical Histogram

    题意:按样例那样模拟…… 解法:模拟…… 代码: #include<stdio.h> #include<iostream> #include<algorithm> ...

  4. Openjudge 1.3-04 垂直直方图

    04:垂直直方图 查看 总时间限制: 1000ms 内存限制: 65536kB 描述 输入4行全部由大写字母组成的文本,输出一个垂直直方图,给出每个字符出现的次数.注意:只用输出字符的出现次数,不用输 ...

  5. J - Vertical Histogram(1.5.7)

    J - Vertical Histogram(1.5.7) Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d &am ...

  6. Openjudge-NOI题库-垂直直方图

    题目描述 Description 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. ...

  7. 打表格,字符串处理,POJ(2136)

    题目链接:http://poj.org/problem?id=2136 水题WA了半天,结果是数组开小了. #include <stdio.h> #include <string.h ...

  8. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  9. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

随机推荐

  1. 九度OJ 1198:a+b (大数运算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6745 解决:2320 题目描述: 实现一个加法器,使其能够输出a+b的值. 输入: 输入包括两个数a和b,其中a和b的位数不超过1000位 ...

  2. H5动静分离

    1. 动静分离的实现思路(类似于iOS.安卓的思路,后台提供数据接口,前端用ajax异步请求json数据,再把json数据渲染到页面) 动静分离是将网站静态资源(HTML,JavaScript,CSS ...

  3. OutOfMemoryError: Java heap space和GC overhead limit exceeded在Ant的Build.xml中的通用解决方式

    这个仅仅是一点点经验,总结一下,当中前两个相应第一个Error.后两个相应第二个Error,假设heap space还不够.能够再改大些. <jvmarg value="-Xms512 ...

  4. 基于Spring框架的Shiro配置(转发:http://kdboy.iteye.com/blog/1103794)

    一.在web.xml中添加shiro过滤器 <!-- Shiro filter--> <filter> <filter-name>shiroFilter</f ...

  5. angularjs 中的iframe 标签 ng-src 路径 z-index 必须有position

    如果直接写路径到iframe标签里的ng-src中会出现报错: 解决方法: 1.ng里面有个属性是专门用来解决跨域问题的 $sce. 用法: $scope.someUrl = $sce.trustAs ...

  6. HTML 5 本地存储

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  7. 使用cocoaPods import导入时没有提示的解决办法

    1.选择target(就是左边你的工程target)—— BuildSettings —— search Paths 下的 User Header Search Paths(如图所示:)    2.双 ...

  8. P4965 薇尔莉特的打字机

    题目 P4965 薇尔莉特的打字机 快到十二点了正在颓废突然发现了一道好题 虽然毒瘤,但确实是容斥原理的好题啊,做法也特别巧妙(标程 思路 题目大意(怕自己突然忘) n个初始字符,m个操作(加入或删除 ...

  9. 第二篇、HTML

    一.html文档树 二.HTML分类 块级标签和内联标签: 块级标签:<p><h1><table><ol><ul><form>& ...

  10. org.eclipse.core.resources.bak文件导致MyEclipse每次关闭时无法保存文件

    MyEclipse关闭时提示如下信息 Problems occurred while trying to save the state of the workbench. Internal Error ...