Poj 2136 Vertical Histogram(打印垂直直方图)
一、Description
digits, or punctuation) appears in the all-upper-case input. Format your output exactly as shown.
Input
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(当时写的比较恶心,优化一下)
Vertical Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21223 Accepted: 10048 ...
- poj 2136 Vertical Histogram 解题报告
题目链接:http://poj.org/problem?id=2136 题意不难理解,就是输入四行字符串(每行字符总数不超过72个),统计26个英文字母的数目,并按柱状图的形式输出.我的思路就是,先用 ...
- POJ 2136 Vertical Histogram
题意:按样例那样模拟…… 解法:模拟…… 代码: #include<stdio.h> #include<iostream> #include<algorithm> ...
- Openjudge 1.3-04 垂直直方图
04:垂直直方图 查看 总时间限制: 1000ms 内存限制: 65536kB 描述 输入4行全部由大写字母组成的文本,输出一个垂直直方图,给出每个字符出现的次数.注意:只用输出字符的出现次数,不用输 ...
- J - Vertical Histogram(1.5.7)
J - Vertical Histogram(1.5.7) Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- Openjudge-NOI题库-垂直直方图
题目描述 Description 写一个程序从输入文件中去读取四行大写字母(全都是大写的,每行不超过72个字符),然后用柱状图输出每个字符在输入文件中出现的次数.严格地按照输出样例来安排你的输出格式. ...
- 打表格,字符串处理,POJ(2136)
题目链接:http://poj.org/problem?id=2136 水题WA了半天,结果是数组开小了. #include <stdio.h> #include <string.h ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
随机推荐
- spring+hibernate+springmvc整合框架搭建
搭建maven web项目这里不再讲述,详情请查看http://www.cnblogs.com/wql025/p/5215570.html 现在讲述项目的搭建过程. 1.通过maven导入项目所用的j ...
- (转)jquery $.proxy的使用
在某些情况下,我们调用Javascript函数时候,this指针并不一定是我们所期望的那个.例如: 1 //正常的this使用 2 $('#myElement').click(function() { ...
- Javaweb基础--->过滤器filter(转发)
一.Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- django 异步任务实现及Celery beat实现定时/轮询任务
Celery定时任务 requirements celery==3.1.25 异步任务 django-celery==3.2.2 定时任务管理包 redis==2.10.6 django-redis- ...
- Python socket TCPServer Demo
#coding:utf-8 from SocketServer import TCPServer,BaseRequestHandler import traceback class MyBaseReq ...
- LeetCode:字符串的排列【567】
LeetCode:字符串的排列[567] 题目描述 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列. 换句话说,第一个字符串的排列之一是第二个字符串的子串. 示例1: ...
- Android AbsoluteLayout绝对布局
绝对布局也叫坐标布局,指定元素的绝对位置,因为适应性很差,一般很少用到.可以使用RelativeLayout替代. 常用属性: android:layout_x --------组件x坐标 andro ...
- 第十篇、模块一、sys\os\hashlib模块的应用
一.模块分为三种 1)自定义模块 2)第三方模块 3)内置模块 如何导入模块? 下面两种: 1)import 模块名字 as 别名(重新给模块命名) 2)from 模块名字 import 功能( ...
- Android系统篇之—-编写系统服务并且将其编译到系统源码中【转】
本文转载自:http://www.wjdiankong.cn/android%E7%B3%BB%E7%BB%9F%E7%AF%87%E4%B9%8B-%E7%BC%96%E5%86%99%E7%B3% ...
- Docker 搭建本地Registry
Docker已经将Registry开源,Registry本身也是一个容器. 1. 修改配置/etc/docker/daemon.json,去掉docker默认的https的访问 里面的内容是一个j ...