题目类型:字符串
题目来源:https://www.luogu.org/problemnew/show/P1598
思路:统计,然后从柱状图的最高点开始输出,一行一行输出
代码:
//
// Created by Leo Lee on 2019/6/13.
//
#include <iostream>
#include <string>
#include <string.h>
#include <algorithm>
using namespace std;
char str[120];
int counchar[26];
void coun();
void output();
int main(){
for(int i = 0;i<4;i++){
scanf("%[^\n]",str);//以回车为字符串读取结束字符
getchar();//吃掉回车,不然下一次一开始读就结束了
coun();
}
output();
return 0;
}
void coun(){//统计
for(int i = 0;i<strlen(str);i++){
if(65<=str[i]&&str[i]<=90)
counchar[str[i]-65]++;
}
}
void output(){
int maxe = *max_element(counchar,counchar+26);
for(int i = maxe;i>0;i--){
for(int j = 0;j<26;j++){
if(i == counchar[j]){
putchar('*');
counchar[j]--;
} else{
putchar(' ');
}
if(j!=25){
putchar(' ');
}
}
cout<<endl;
}
for(char c = 'A';c<='Z';c++){
putchar(c);
if(c!='Z')
putchar(' ');
}
}