求垂直直方图 输入四行大写字母打印每个字母分别出现的次数个*

题目描述

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.

输入

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

输出

* 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. 

样例输入

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

样例输出

                            *
                            *
        *                   *
        *                   *     *   *
        *                   *     *   *
*       *     *             *     *   *
*       *     * *     * *   *     * * *
*       *   * * *     * *   * *   * * * *
*     * * * * * *     * * * * *   * * * *     * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
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

算法实现:

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
	int word[26]={0};
	char a[4][72]={0};
	for(int i=0;i<4;i++)
	cin.getline(a[i],72);
	for(int i=0;i<4;i++) 
	{
		for(int j=0;j<strlen(a[i]);j++)
		{
			if(a[i][j]<='Z'&&a[i][j]>='A')
			word[a[i][j]-'A']++;
		}
	}
	int max=0;
	
	//找字母数最多的那个 
	for(int i=0;i<26;i++)
	if(word[i]>max) max=word[i];
    
    //列一个字符数组来存储图形 
	char ch[72][51]={0};
	
	for(int i=0;i<72;i++)
	for(int j=0;j<51;j++)
	ch[i][j]=' ';
	
	for(int i=0;i<51;i=i+2)
	for(int j=max-1;j>=0;j--)
	{
		if(word[i/2]>0)
		{
			ch[i][j]='*';
			word[i/2]--;
		}
	}
	
	//打印图形 
	for(int i=0;i<max;i++)
	{
	    for(int j=0;j<51;j++)
	    cout<<ch[j][i];
	    cout<<endl;
	}
	char p='A';
	for(int j=0;j<51;j++)
	{
		if(j%2==0)
		{
    		cout<<p;
	    	p++;
		 }
		 else
		 cout<<' '; 
	}
	return 0;	
}


猜你喜欢

转载自blog.csdn.net/qq_41486817/article/details/80617314