Luogu brush questions C++ language | P1598 vertical histogram

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


[Title description]

Write a program to read four lines of uppercase letters from an input file (all in uppercase, no more than 100 characters per line), and then use a histogram to output the number of times each character appears in the input file. Arrange your output format strictly according to the output sample.

【enter】

Four lines of characters, consisting of uppercase letters, each line not to exceed 100 characters

【Output】

It consists of several lines, the first few lines are composed of spaces and asterisks, and the last line is composed of spaces and letters. Do not print unwanted extra spaces at the end of any line. Do not print any empty lines.

【Input sample】

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

【Output sample】

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

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int a[300]={0}, max=0;
    string s;
    for (int i=1; i<=4; i++) {
        getline(cin ,s);
        for (int j=0; j<s.length(); j++) {
            a[s[j]]++;
        }
    }
    for (int i='A'; i<='Z'; i++) {
        if (a[i]>max) max = a[i];
    }
    for (int i=max; i>0; i--) {
        for (int j='A'; j<'Z'; j++) {
            if (a[j]>=i) cout << "* ";
            else cout << "  ";
        }
        if (a['Z']>=i) cout << "*";
        else cout << " ";
        cout << endl;
    }
    for (char i='A'; i< 'Z'; i++) {
        cout << i << " ";
    }
    cout << 'Z';
    return 0;
}

【operation result】

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

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132774717