以上是朋友圈中一奇葩贴:“2月14情人节了,我决定造福大家。第2个赞和第14个赞的,我介绍你俩认识…………咱三吃饭…你俩请…”。现给出此贴下点赞的朋友名单,请你找出那两位要请客的倒霉蛋。
输入格式:
输入按照点赞的先后顺序给出不知道多少个点赞的人名,每个人名占一行,为不超过10个英文字母的非空单词,以回车结束。一个英文句点.标志输入的结束,这个符号不算在点赞名单里。
输出格式:
根据点赞情况在一行中输出结论:若存在第2个人A和第14个人B,则输出“A and B are inviting you to dinner...”;若只有A没有B,则输出“A is the only one for you...”;若连A都没有,则输出“Momo... No one is for you ...”。
#include<iostream>
using namespace std;
int main() {
string s, c, x;
int i = 0;
while (cin >> s && s != ".") {
// int i = 0;应该在循环外边
i += 1;
if (i == 2)
c = s;
if (i == 14)
x = s;
}
if (c.empty())
cout << "Momo... No one is for you ..." << endl;
if (!c.empty() && x.empty())
cout << s << " is the only one for you..." << endl;
if (!c.empty() &&!x.empty())
cout << c << "and" << x << "are inviting you to dinner... " << endl;
}
//`c.empty()`是一个C++中的字符串成员函数,用于判断字符串`c`是否为空。如果`c`为空,则该函数返回`true`,否则返回`false`。
//下面这段运行错误是因为!C不对,要使用应该是c是bool类型或者为可以隐式转为bool类型,
//int类型可以直接使用,string类型的加!c.empty()
/* if (!c)
cout << "Momo... No one is for you ..." << endl;
if (c && !x)
cout << s << " is the only one for you..." << endl;
if (c && x)运算符报错是因为条件表达式的类型错误,例如`if (a && b)`,其中a和b的类型不是bool类型,这样会导致编译器无法正确执行逻辑运算,从而报错。
cout << c << "and" << x << "are inviting you to dinner... " << endl;
*/