0%

北大 acm 2136 Vertical Histogram 解题报告

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10654 Accepted: 5029

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 YOURHISTOGRAM 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

题意:计算各个字母出现的次数,然后依次打出次数个*

代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>

using namespace std;

int main()
{
int count[26]={0};
int i,n,max,j;
char str[100],st[300];

while(gets(str)&&str[0]!='\0')
{
st[0]='\0';
max=0;

strcat(st,str);

for(i=1;i<4;i++)
{
gets(str);
strcat(st,str);
}

for(i=0;st[i]!='\0';i++) //这个循环计算每个字母出现几次
{

if(st[i]<65||st[i]>90)
continue;

n=st[i]-'A';

count[n]++;

if(count[n]>max)
max=count[n]; //计算出现最多的次数
}

for(i=max;i>0;i--) //从最多的次数max,依次减小
{
for(j=0;j<26;j++) //对26个字母进行比较,如果于次数i相等则输出*,否则输出空格
if(count[j]==i)
{
cout<<"* ";
count[j]--;
}
else
cout<<" ";

cout<<endl;
}

for(i=0;i<26;i++) //输出最底下的26个字母
printf("%c ",'A'+i); cout<<endl;

}

return 0;
}
谢谢您的打赏,我的大英雄 ^_^
Thank you for your generosity, my big hero ^_^

更多内容请关注公众号「西小玛」