文章

15

粉丝

27

获赞

0

访问

1.9k

头像
字母频率 题解:想问一下有没有大佬知道,为什么加了while就只能过66%
P1019 贵州大学机试题
发布于2024年4月3日 15:12
阅读数 157

#include <bits/stdc++.h>
using namespace std;

int main(){
	string s;
	while(getline(cin,s)){
		for(int i=0;i<s.size();i++){
			if(s[i]>='A'&&s[i]<='Z')s[i]=s[i]-'A'+'a';
		}
		map<char,int> sheet;
		for(int i=0;i<s.size();i++){
			if(s[i]>='a'&&s[i]<='z')sheet[s[i]]++;
		}
		char get;
		int max=0;
		for(const auto &pair:sheet){
			if(pair.second>max){
				get=pair.first;
				max=pair.second;
			}
		}
		cout<<get<<" "<<max<<endl;
	}
	return 0;
}

上面的代码如果把while循环去掉就能ac,不知道为什么,希望有知道的大佬解惑

登录查看完整内容


登录后发布评论

2 条评论
snake
2024年4月3日 17:49

因为这个代码如果直接回车的话会出现这种输出

赞(0)

RingoCrystal : 回复 snake: 原来如此,谢谢大佬

2024年4月3日 18:40