解题报告:
数据读入+处理大致为整行读入,按每一个字符统计各个字母出现的次数,存入预先定义的数组,并比较出最大值。
到这里我直接将结果输出了,我们已经知道输出必定有MAX + 1行,其中MAX为出现最多的那个字母出现的次数,1是因为最下面一行是字母表。
于是我们可以对每行判断:将行标号为MAX+ 1,MAX,...,2,1。该输出第i行第j列的时候检验序号为j的字母出现次数是否达到了j,如果是,则输出‘*’,否则输出空格。
另外注意,列与列间有一个空列(全是空格),我就因为这个Presentation Error了一次,另外用到memset需要include<memory.h>,那次Compile Error就是因为这个。
AC代码:
#include <iostream>
#include <memory.h>
#include <string.h>
#include <cstdlib>
using namespace std;
int main()
{
int i, j;
int res[26];
char buffer[81];
memset(res, 0, sizeof(res));
for(i = 0; i != 4; ++i)
{
cin.getline(buffer, sizeof(buffer));
int len = strlen(buffer);
for(j = 0; j != len; ++j)
{
if(isalpha(buffer[j]))
{
res[buffer[j] - 'A']++;
}
}
}
int maxres = 0;
for(i = 0; i != 26; ++i)
{
if(res[i] > maxres)
{
maxres = res[i];
}
}
for(i = 0; i != maxres; ++i)
{
for(j = 0; j != 26; ++j)
{
if(j > 0)
{
cout << " ";
}
if(res[j] >= maxres - i)
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
cout << "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" << endl;
return 0;
}
下面贴上题(图片格式):