Vertical Histogram
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
题意: 给出4行字符串,每行字符串不超过72个字符,让你统计这4行字符串中26个大写字母的数量,一个字母用一个’*'表示,输出如样例所示,最后一行没有回车,字母间用空格隔开。
简单模拟题,AC代码如下:(选择G++编译器)
#include <stdio.h>
#include <string.h>
int main(){
char s[75];
int a[27];
memset(a, 0, sizeof(a));
for(int i = 0; i < 4; i++){
scanf("%[^\n]", s);
int len = strlen(s);
for(int j = 0; j < len; j++){
if(s[j] >= 'A' && s[j] <= 'Z'){
a[s[j] - 'A']++;
}
}
getchar();
}
int max = -1;
for(int i = 0; i < 26; i++){
if(a[i] > max) max = a[i];
}
for(int i = 0; i <= max; i++){
for(int j = 0; j < 26; j++){
if(i == max){
if(j == 25) printf("Z");
else
printf("%c ", 'A' + j);
}
else {
if(j == 25){
if(a[j] >= max - i) printf("*");
else printf(" ");
}
else
if(a[j] >= max - i) printf("* ");
else printf(" ");
}
}
if(i != max) printf("\n");
}
return 0;
}
End