Vertical Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17681 | Accepted: 8546 |
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
Source
题意:给你四行字符串,让你垂直的输出A-Z的数量,用*来表示。
做法:先统计好各个字母的数量,并记录最大值max,然后最大值就是行数,然后从A-Z从max行开始--到第一行为止记录,最后输出即可。
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
int main()
{
int i,j,n;
char s[105];
int num[105];
int max1;
max1=0;
char ans[505][50]={};
memset(num,0,sizeof(num));
int t=4;
while(t--)
{
gets(s);
n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]>='A'&&s[i]<='Z')
{
num[s[i]-'A']++;
if(num[s[i]-'A']>max1)
max1=num[s[i]-'A'];
}
}
}
for(i=0;i<26;i++)
{
for(j=0;j<num[i];j++)
{
ans[max1-j][i]='*';
}
}
for(i=1;i<=max1;i++)
{
for(j=0;j<26;j++)
{
if(j!=0)
printf(" ");
if(ans[i][j]=='*')
printf("%c",ans[i][j]);
else
printf(" ");
}
printf("\n");
}
printf("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\n");
return 0;
}