Vertical Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20557 | Accepted: 9780 |
Description
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 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
计算各字母的频率数组num和最高频率maxn(行数)
自上而下画统计图,进入for(int i=0;i<maxn;i++)的循环,搜索num【0】到num【25】,若(num[j]+i>=maxn)则输出“*”否则输出“ ”。
#include<iostream>
#include<string>
using namespace std;
int num[26]={0};
void ff(string ss)
{
int l,n;
l=ss.length();
for(int i=0;i<l;i++)
{
n=ss[i]-'A';
if(n>=0&&n<26)
num[n]++;
}
}
int main()
{
string str[4];int maxn=0;
for(int i=0;i<4;i++)
{
getline(cin,str[i]);
}
for(int i=0;i<4;i++)
{
ff(str[i]);
}
for(int i=0;i<26;i++)
{
if(num[i]>maxn) maxn=num[i];
}
// cout<<maxn<<endl;
for(int i=0;i<maxn;i++)
{
for(int j=0;j<26;j++)
{
if((num[j]+i>=maxn)) cout<<"*";
else cout<<" ";
if(j!=25) cout<<" ";
else cout<<endl;
}
}
cout<<"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"<<endl;
return 0;
}