Poj 2136 Vertical Histogram(打印垂直直方图)
2013-08-04 19:15
519 查看
一、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
Sample Output
二、题解
目标:计算出给定的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代码
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(打印垂直直方图)
- POJ 2136 Vertical Histogram(垂直直方图)
- poj&nbsp;百炼&nbsp;2800:垂直直方图
- 习题1-13(垂直方向直方图):编写一个程序打印输入中单词长度的垂直方向的直方图。
- 第7周 C语言程序设计(新2版) 练习1-13 打印输入单词长度的直方图(水平与垂直)
- 编写一个程序,打印输入中单词长度的水平和垂直方向直方图
- 打印输入中单词长度的直方图(垂直方向的直方图)
- 练习 1 - 13 编写一个程序,打印输入中单词长度的水平直方图 和 垂直直方图
- C语言打印水平直方图或者垂直直方图
- POJ 百炼 保研机试 2800:垂直直方图
- POJ 2800 垂直直方图 解题报告
- 打印输入中单词长度的直方图(垂直方向)
- 练习1-13 打印水平或垂直直方图
- 练习1-13 编写一个程序,打印输入中单词长度的垂直方向的直方图(垂直图)
- 打印输入中单词长度的直方图,垂直方向的。折磨使我强大的一个程序!
- poj:垂直直方图
- 打印输入中单词长度的直方图(垂直打印)
- 打印输入中单词长度直方图 (垂直方向) c程序书中的练习题,上机无书下输入,调试
- C程序设计语言,练习1-13,编写一个程序,打印输入单词的直方图(垂直方向)
- POJ 2136 Vertical Histogram 打印特殊图形