您的位置:首页 > 其它

04:垂直直方图

2017-04-17 10:30 302 查看
描述
 输入4行全部由大写字母组成的文本,输出一个垂直直方图,给出每个字符出现的次数。注意:只用输出字符的出  现次数,不用输出空白字符,数字或者标点符号的输出次数。

输入输入

 包括4行由大写字母组成的文本,每行上字符的数目不超过80个。输出输出包括若干行。其中最后一行给出26个  大写英文字母,这些字母之间用一个空格隔开。前面的几行包括空格和星号,每个字母出现几次,就在这个字母 的上方输出一个星号。注意:输出的第一行不能是空行。

样例输入

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

来源

  翻译自USACO 2003 February Orange的试题。

代码

 

1 头文件略
2 using namespace std;
3 char a[4][80];
4 int n[26]={0};
5 int main()
6 {
7     int max=0;
8     for(int i=1;i<=4;i++)
9     {
10         gets(a[i]);
11         int A=strlen(a[i]);
12         for(int j=0;j<A;j++)
13         {
14             if(a[i][j]==' ') continue;
15             else n[a[i][j]-63]++;
16         }
17     }
18     for(int i=2;i<=27;i++)
19     {
20         max=max>n[i]?max:n[i];
21     }
22     for(int i=max;i>=1;i--)
23     {
24         for(int j=2;j<=27;j++)
25         {
26             if(n[j]>=max) cout<<'*'<<' ';
27             else if(max>n[j]) cout<<' '<<' ';
28         }
29         cout<<endl;
30         max--;
31     }
32     cout<<"A B C D E F G H I ";
33     cout<<"J K L M N O P Q R ";
34     cout<<"S T U V W X Y Z "<<endl;
35     return 0;
36 }


 Other verson:

1 #include"stdafx.h"
2 #include<iostream>
3 #include<cstdio>
4 #include<string>
5 #include<cstring>
6 using namespace std;
7 int a[30];
8 int main()
9 {
10     int temp = 4, max = 0;
11     while (temp--)
12     {
13         char ch[200];
14         gets_s(ch);
15         int l = strlen(ch);
16         while (l--) if (ch[l] <= 'Z' && ch[l] >= 'A')  a[ch[l] - 'A']++;
17     }
18     temp = 26;
19     while (temp--)max = max > a[temp] ? max : a[temp];
20     while (max--)
21     {
22         temp = 26;
23         while (temp--)
24         {
25             if (a[25 - temp] >= max+1) cout << "* ";
26             else cout << "  ";
27         }
28         cout << endl;
29     }
30     temp = 26;
31     while (temp--) printf("%c ", 26 - temp + 64);
32     cout << endl;
33     return 0;
34 }


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: