PATB1029: 字符统计
【思路】:
HASH经常用来求两个集合的交 集 补集等 。去重小能手
1首先对目标集合中的元素进行HASH,这里面试大小写不区分,且只需要输出小写,所以统一转换成小写
isalpha(s[i])判断是字母吗
2然后对元集合进行过滤性输出 求最大值 老方法 还是设置一个maxID ,这里需要注意的是。索引需要换成char类型的输出
printf("%c %d", maxId +'a', Hash_time[maxId]); //maxId +'a' 输出字符
【参考答案】
void B1042(){
char s[1010];
gets(s);
int Hash_time[26] = {0};
for (int i = 0; i < strlen(s); i++)
{
if (isalpha(s[i]))
{
s[i] = towlower(s[i]);
Hash_time[s[i] -'a']++;
}
}
//求Hash_time的最大值
int maxId=0;
for (int i = 0; i < 26; i++)
{
if (Hash_time[maxId] < Hash_time[i])
{
maxId = i;
}
}
printf("%c %d", maxId +'a', Hash_time[maxId]); //maxId +'a' 输出字符
}