Vertical Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18342 | Accepted: 8849 |
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
方法1:
/***********************************************************************
Copyright (c) 2015,wangzp
All rights no reserved.
Name: 《Vertical Histogram》In PEKING UNIVERSITY ACM
ID: PROBLEM 2136
问题简述: 根据终端输入的大写字母,统计字母的个数并打印直方图。
Date: Aug 12, 2015
***********************************************************************/
#include
int main(void)
{
int i,k,n,j;
int max;
char a[73];
int num[26] = {0};
for(i = 0; i < 4; i++)
{
scanf("%[^\n]s",a);
getchar();
j = 0;
while(a[j] != '\0')
{
switch (a[j])
{
case 'A':
num[0]++;
break;
case 'B':
num[1]++;
break;
case 'C':
num[2]++;
break;
case 'D':
num[3]++;
break;
case 'E':
num[4]++;
break;
case 'F':
num[5]++;
break;
case 'G':
num[6]++;
break;
case 'H':
num[7]++;
break;
case 'I':
num[8]++;
break;
case 'J':
num[9]++;
break;
case 'K':
num[10]++;
break;
case 'L':
num[11]++;
break;
case 'M':
num[12]++;
break;
case 'N':
num[13]++;
break;
case 'O':
num[14]++;
break;
case 'P':
num[15]++;
break;
case 'Q':
num[16]++;
break;
case 'R':
num[17]++;
break;
case 'S':
num[18]++;
break;
case 'T':
num[19]++;
break;
case 'U':
num[20]++;
break;
case 'V':
num[21]++;
break;
case 'W':
num[22]++;
break;
case 'X':
num[23]++;
break;
case 'Y':
num[24]++;
break;
case 'Z':
num[25]++;
break;
default:
break;
}
j++;
}
}
max = num[0];
//找到个数最大的
for (k = 1; k <= 26; k++)
{
if (num[k] >= max)
{
max = num[k];
}
else
{
/*do nothing*/
}
}
for(n = max;n >= 1;n--)
{
for (k = 0; k < 26; k++)
{
if (num[k] < n)
{
printf(" ");
}
else
{
num[k]--;
printf("* ");
}
}
printf("\n");/*最后一行前的回车*/
}
for(i='A';i<='Z';i++)
{
printf("%c ",i);
}
return 0;
}
方法2:
/***********************************************************************
Copyright (c) 2015,wangzp
All rights no reserved.
Name: 《Vertical Histogram》In PEKING UNIVERSITY ACM
ID: PROBLEM 2136
问题简述: 根据终端输入的大写字母,统计字母的个数并打印直方图。
Date: Aug 12, 2015
***********************************************************************/
#include"stdio.h"
#include"string.h"
int main()
{
char a[110];
int map[100]={0};
int i,j,max=0;
for(i=0;i<4;i++)
{
gets(a);
for(j=0;j
='A'&&a[j]<='Z')
{
map[a[j]]++;
if(max
=1;j--)
{
for(i='A';i<='Z';i++)
{
if(map[i]