POJ-2136 Vertical Histogram-用*號統計字母個數

kewlgrl發表於2015-08-07
Vertical Histogram
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18160   Accepted: 8771

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

Source


#include <iostream>
#include <cstring>
#include <iomanip>
#include <stdio.h>
#include <cmath>
using namespace std;

int main()
{
    char a[72],b[72],c[72],d[72];//分別用來存四行字母串
    int s[26],i,max,j;
    memset(s,0,sizeof(s));
    gets(a);
    for(i=0; a[i]!='\0'; ++i)
        ++s[int(a[i]-'A')];
    gets(b);
    for(i=0; b[i]!='\0'; ++i)
        ++s[int(b[i]-'A')];
    gets(c);
    for(i=0; c[i]!='\0'; ++i)
        ++s[int(c[i]-'A')];
    gets(d);
    for(i=0; d[i]!='\0'; ++i)
        ++s[int(d[i]-'A')];
    max=s[0];
    for(i=1; i<26; ++i)
        if(s[i]>max)
            max=s[i];
    for(i=0; i<max; ++i)
    {
        for(j=0; j<26; ++j)
        {
            if(s[j]>=(max-i))//這句是個小技巧,自上而下判斷輸出
                cout<<"* ";
            else
                cout<<"  ";
        }
       cout<<endl;
    }
    cout<<"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"<<endl;
    return 0;
}


相關文章