输入4行全部由大写字母组成的文本,输出一个垂直直方图,给出每个字符出现的次数。注意:只用输出字符的出现次数,不用输出空白字符,数字或者标点符号的输出次数。
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
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int i,j,cs[27],maxn,n=4; char s[27][35001],w[81],b[27][35001]; int main() { while (n--) { gets(w); for(i=0;i<strlen(w);++i) { if(w[i]>=65&&w[i]<=90) { cs[w[i]-65]++; maxn=max(maxn,cs[w[i]-65]); } } } for(i=0;i<26;++i) { for(j=0;j<=maxn;++j) { if(j<=cs[i]) s[i][j]=‘*‘; else s[i][j]=‘ ‘; } } for(i=0;i<26;++i) { for(j=0;j<=maxn;++j) b[j][i]=s[i][j]; } for(i=maxn;i>=1;--i) { for(j=0;j<26;++j) cout <<b[i][j]<<" "; cout<<endl; } for(i=0;i<=25;++i) cout<<char(i+65)<<" "; }
原文:http://www.cnblogs.com/ruojisun/p/6209250.html