百练
2800 垂直直方图
输入样例:
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
THIS IS AN EXAMPLE TO TEST FOR YOUR
HISTOGRAM PROGRAM.
HELLO!
输出样例:
*
*
* *
* * * *
* * * *
* * * * * *
* * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
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.printf(cout) 调试法
代码:
#include<iostream>
using namespace std;
const char B[26] = {'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'};
int main()
{
char str[80]; //not upper to 80 words
int count = 4, max = 0; //four lines
int number[26]={0};
while(count--)
{
cin.getline(str, 80);
int j = 0;
while(str[j])
{
for(int i = 0; i<26 ; i++)
{
if(str[j] == B[i])
number[i] ++; //number of corresponding letter increased;
}
j++;
}
}
for(int i = 0; i<26 ; i++)
{
if(number[i] > max)
max = number[i]; //choose the max number;
//cout<<number[i]<<endl;
}
for(int i = max; i>0 ; i--)
{
for(int j = 0; j<26 ; j++)
{
if(number[j] >= i)
cout<<"* "; //number of corresponding letter increased;
else
cout<<" ";
}
cout<<endl;
}
for(int j = 0; j<26 ; j++)
{
if(j != 25)
cout<<B[j]<<" ";
else
cout<<B[j];
}
return 0;
}