P1598 垂直柱状图解法

· · 题解

比较简单的题,大家思路都差不多,但觉得一些地方不用写太麻烦,特别是读取过程,getchar多少行都能读。另外题目里写到每行最后不能有多余空格好像大家都不理会,测试数据好像没考虑这个情况,反正这里先给出个方法,欢迎指点。

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    char c;
    int a[26] = {0};
    int max = 0;
    while((c = getchar()) != EOF)
        if(c >= 'A' && c <= 'Z')
            a[c-'A']++;
    for(int i=0; i<26; i++)
        if(max < a[i])
            max = a[i];
    while(max){
        for(int i=0; i<26; i++){
            if(i) cout<<" ";
            if(a[i] >= max) cout<<"*";
            else cout<<" ";
        }
        cout<<endl;
        max--;
    }
    for(int i=0; i<26; i++){
        if(i) cout<<" ";
        printf("%c", i + 'A');
    }
    cout<<endl;
    return 0;
}