Vertical Histogram
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1374 Accepted Submission(s): 240
Problem 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/* 思路:解法很简单,坑爹的是格式,~~~~(>_<)~~~~ ,为了格式浪费n多的时间 好了,下面说一下输出的格式 *(\n) *(\n) * *(\n) * * * *(\n) * * * *(\n) * * * * * *(\n) * * * * * * * * * *(\n) * * * * * * * * * * * * *(\n) * * * * * * * * * * * * * * * * * *(\n) * * * * * * * * * * * * * * * * * * * * * * * * * *(\n) 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) (\n)后面没有空格填充了哦, */ #include<iostream> using namespace std; int main(void) { char a[78]; int b[27],count=0; memset(b,0,sizeof(b)); while(gets(a)){ count++; int i,len=strlen(a); for(i=0;i<len;i++) if(a[i]>='A'&&a[i]<='Z') b[a[i]-'A']++; int max=0; for(i=0;i<26;i++) max=max<b[i]?b[i]:max; if(count==4){ count=0; int num=max,m=0; for(i=0;i<max;i++){ for(int k=25;k>=0;k--) //从第26个字母往前遍历,找到满足b[k]==num, //即这个地方的后面就是换行,后面没有空格了 if(b[k]>=num) break; for(int j=0;j<26;j++){ if(b[j]>=num) cout<<"*"; else{ if(j<k) cout<<" "; else break; } if(j<k) cout<<" "; } num--; cout<<endl; } for(i=0;i<26;i++){ cout<<(char)('A'+i); if(i<25) cout<<" "; } cout<<endl; memset(b,0,sizeof(b)); } } return 0; }