while(cin>>T)这种方法为什么过不了
查看原帖
while(cin>>T)这种方法为什么过不了
858646
Syihang楼主2024/3/23 20:06
求教,为什么这种方法过不了。样例都能过,但是提交的时候只有答案为-1时才能通过,其他数据全是WA
#include<iostream>
#include<cstring>
using namespace std;
string toUpper(string str){
	for(auto &x:str) {
		x = toupper(x);
	}
	return str;
}
int main() {
	string P;
	string T;
	cin>>P;
	int cnt = 0, loc = 0, i = 0;
	while(cin>>T) {
		if(toUpper(P) == toUpper(T)) {
			cnt ++;
			if(cnt == 1) loc = i;
		}
		i = i + T.size() + 1;
//		cout<<P<<" "<<T<<" "<<i<<" "<<cnt<<" "<<loc; 
	}
	if(cnt != 0) cout<<cnt<< " " <<loc;
	else cout<<"-1";
	return 0;
} 

2024/3/23 20:06
加载中...