前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU2708 - Vertical Histogram题解

HDU2708 - Vertical Histogram题解

作者头像
天道Vax的时间宝藏
发布2021-08-11 10:37:30
2600
发布2021-08-11 10:37:30
举报
文章被收录于专栏:用户5305560的专栏

题目:

代码语言:javascript
复制
   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-Z 26个大写字母和其他字符组成,但你要做的只是统计每个大写字母的出现次数,并以图示的方式用*的个数表示出来。

注意事项:

  • 直方图中每个字符后紧接着一个空格。
  • 每行最后一个*号后仅有一个紧挨在一起的空格,无多余其他空格。
  • 字母Z后无空格,无换行。
代码语言:javascript
复制
#include <iostream>  
#include <string>  
#include <cstring>  
#include <cctype>  
using namespace std;  
const int MAXN = 26;  
int acount[MAXN];  
  
int main()  
{  
    int linecount, max;  
    string s;  
  
    memset(acount, 0, sizeof(acount));  
  
    linecount = 0;  
    while (getline(cin, s)) {  
        // 统计字母  
        for(int i=0; i<(int)s.size(); i++)  
//            if(isalpha(s[i]))  
//                acount[s[i] - 'A']++;  
            if(isupper(s[i]))  
                acount[s[i] - 'A']++;  
  
        // 每4行输出一次结果  
        if(++linecount == 4) {  
            linecount = 0;  
  
            // 计算最大的统计值  
            max = 0;  
            for(int i=0; i<MAXN; i++)  
                if(acount[i] > max)  
                    max = acount[i];  
  
            // 输出max行  
            for(int i=max; i>0; i--) {  
                for(int j=0; j<MAXN; j++) {  
                    if(acount[j] >= i)  
                        cout << "* ";  
                    else  
                        cout << "  ";  
                }  
                cout << endl;  
            }  
  
            for(int i=0; i<MAXN; i++)  
                cout << (char)('A' + i) << " ";  
            cout << endl;  
        }  
    }  
  
    return 0;  
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档