
#include iostream #include string #include sstream using namespace std; // 判断单词是否包含字母a bool hasA(string word) { for (char ch : word) { if (ch a) return true; } return false; } int main() { cout 输入规则输入一行英文句子以英文句号 . 结尾 endl; cout 请输入字符串; string line; // 读取整行输入包含空格 getline(cin, line); // 去掉末尾的句号 line.pop_back(); stringstream ss(line); string word; string ans ; // 保存结果单词 // 循环拆分每个单词 while (ss word) { // 当前单词包含a if (hasA(word)) { // 结果为空 或者 当前单词更长则更新结果 if (ans.empty() || word.size() ans.size()) { ans word; } } } cout \n查找结果; if (ans.empty()) { cout NO endl; } else { cout ans endl; } return 0; }