Vertical Histogram(POJ-2136)
Problem 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
思路:与垂直柱状图(洛谷-P1598 )同一题
Source Program
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
    char word[4][100];		
    int statistic[27]={0};
    int i,j,k;
    int max=0;
 
    for(i=0;i<4;i++)//输入四行字母
    gets(word[i]);
 
 
    for(i=0;i<4;i++)//统计字母个数
        for(j=0;word[i][j]!='\0';j++)
            for(k=1;k<=26;k++)
                if(int(word[i][j]-64)==k)
                    statistic[k]++;
 
    for(i=1;i<=26;i++)//求最大值
        if(max<statistic[i])
            max=statistic[i];
    max++;
    
    char column[28][100];
    for(i=1;i<=26;i++)	column[i][0]=char(i+64);//存储横坐标的字母
 
    for(i=1;i<=26;i++)//按字母个数统计“*”
    {
        for(j=1;j<=max;j++)
        {
            if(j<=statistic[i])	column[i][j]='*';
            else	column[i][j]=' ';
        }
    }
 
     for(j=max-1;j>=0;j--)//按要求输出
     {
         for(i=1;i<=26;i++)
         {
             if(i<26)	cout<<column[i][j]<<" ";
             else cout<<column[i][j];
         }
       	 if(j>0)	cout<<endl;
     }
    return 0;
}
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号