Vertical Histogram

M - Vertical Histogram
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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));,提醒自己一下。


#include<stdio.h>
#include<string.h>
int main()
{
	int i,j,n;
    char ch[4][99];
    int a[30]={0};
    for( i=0;i<4;i++)
        gets(ch[i]);
    for( i=0;i<4;i++)
	{
		n=strlen(ch[i]);
        for(int j=0;j<n;j++)
        {
            a[(int)ch[i][j]-'A']++;
        }
	}
	int max=0;
    for( i=0;i<26;i++)
    {
        if(a[i]>max)
			max=a[i];
    }
	for(i=0; i<max; i++)
    {
        for(j=0; j<26; ++j)
        {
            if(a[j]>=(max-i))
                printf("*");
            else
                printf(" ");
			printf(" ");
        }
		printf("\n");
	}
	printf("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\n");
	return 0;
}
/*THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
THIS IS AN EXAMPLE TO TEST FOR YOUR
HISTOGRAM PROGRAM.
HELLO!*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值