社区讨论

c++,WA3 5 7 8 9,求佬调!!!

P1308[NOIP 2011 普及组] 统计单词数参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@m3uyz3lq
此快照首次捕获于
2024/11/24 10:18
去年
此快照最后确认于
2025/11/04 14:03
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
#include <vector>
#include <limits>
#include <cctype>
using namespace std;
 
 void lo(std::string& str);
 
int main()
{
	string a,b;
	getline(cin,a);//单词 
	string text;
	getline( cin,text );//文章 
	int i,j;
	int c=0;
	lo(a);//转小写 
	lo(text);
	text.insert(text.end(),' ');
	for(i = 0 ; i < text.size() ; i++){
    while(i < text.size() && text[i] == ' '){
        i++;//不是空格就跳出 
    }
    j = i;
    b.clear();//存放文章中单词 
    while(j < text.size() && text[j] != ' '){
        b += text[j];
        j++;
	}//遇到空格就跳出  
    i = j - 1;
    if(a == b){
        c++;//计数器 
    }
}
	if(c!=0){
		cout<<c<<" "<<text.find(a);
	}
	else{
		cout<<-1;
	}
	return 0;
} 


void lo(std::string &str) {
    for (size_t i = 0; i < str.length(); ++i) {
        if (isupper(str[i])) {
            str[i] = tolower(str[i]);
        }
    }
}//copy的转小写函数 

回复

3 条回复,欢迎继续交流。

正在加载回复...