又出现了以回车作为字符串读入结束标志的情况。
上午做的题见:PAT乙级B1033
本题还是比较简单的,注意读入就好,我也因此来写篇博客记录一下。
我的代码:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char str[1005];
char hashTable[128];
int main(){
cin.getline(str,1005,'\n'); //字符串中有空格,要以回车作为读入结束标志
//scanf("%s",&str); 错解
int len=strlen(str);
//英文字母全转换为小写存储
for(int i=0;i<len;i++){
if(str[i]>='A'&&str[i]<='Z')
str[i]+=32;
hashTable[str[i]]++;
}
//找出出现频率最高的字符并输出
char mc='a';
int mn=hashTable['a'];
for(char c='b';c<='z';c++){
if(hashTable[c]>mn){
mc=c;
mn=hashTable[c];
}
}
printf("%c %d",mc,mn);
return 0;
}