题解:P1598 垂直柱状图
模拟即可,难点在于输出。
思路:先统计字母个数,将最多的值存为
上代码:
#include<bits/stdc++.h>
using namespace std;
int a[27];
char s[105];
string ss="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"; //方便后续输出
int main(){
int maxn=-1;
int i,j;
for(i=1;i<=4;i++){
cin.getline(s,105); //整行读入
for(j=0;j<strlen(s);j++){
int t=s[j]-'A'; //强转
if('A'<=s[j]&&s[j]<='Z'){
a[t]++; //类似于桶排
maxn=max(maxn,a[t]);
//求某个字母最多出现几次
}
}
}
for(i=maxn;i>=1;i--){
for(j=0;j<26;j++){
if(a[j]>=i) cout << "* ";
else cout << " ";
//注意每列之间有空格
}
cout << endl; //换行
}
cout << ss << endl;
return 0; //华丽结束
}
其实还是挺水的,~建议评红~。