L1-035 情人节 (15 分)
*文章提供者:海南师范大学 ---- 人工智能教育协会 ---- 张语晨、刘婧怡、秦长风
(一)题目要求
以上是朋友圈中一奇葩贴:“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 …”。
输入样例1:
GaoXZh
Magi
Einst
Quark
LaoLao
FatMouse
ZhaShen
fantacy
latesum
SenSen
QuanQuan
whatever
whenever
Potaty
hahaha
.
输出样例1:
Magi and Potaty are inviting you to dinner...
输入样例2:
LaoLao
FatMouse
whoever
.
输出样例2:
FatMouse is the only one for you...
输入样例3:
LaoLao
.
输出样例3:
Momo... No one is for you ...
(二)代码如下:
方法一:(C)
#include<stdio.h>
#include<string.h>
int main()
{
char s[11],a[11],b[11];
int n=0;
while(~scanf("%s",&s))
{
if(s[0]=='.')break;
n++;
if(n==2) strcpy(a,s);
if(n==14) strcpy(b,s);
}
if(n<2) printf("Momo... No one is for you ...\n");
if(n>=2&&n<14) printf("%s is the only one for you...\n",a);
if(n>=14) printf("%s and %s are inviting you to dinner...\n",a,b);
return 0;
}
方法二:(C++)特定值存储
#include<iostream>
#include<string>
using namespace std;
int main(){
string name, name2, name14;
int count=0;//count用于计算输入名字的个数
while(cin>>name){//while循环依次输入名字
if(name == ".")break;
count++;
if(count==2)name2=name;//将输入的name赋给变量name2
else if(count==14)name14=name;
}
if(count<2) cout<<"Momo... No one is for you ...";
else if(count>=14) cout<<name2<<" and "<<name14<<" are inviting you to dinner...";
else cout<<name2<<" is the only one for you...";
//依次按情况输出
return 0;
}
方法三:(C++)string数组
#include<bits/stdc++.h>
using namespace std;
int main(){
string name[1000];//创建二维数组
//string为字符串数据结构本身是一维,故name[]是二维数组
int i=0;
while(cin >> name[i]){//利用while循环输入数组
if(name[i++] == "."){
break;
}
}
//依情况输出
if(i<3) cout<<"Momo... No one is for you ...";
else if(i>14) cout<<name[1]<<" and "<<name[13]<<" are inviting you to dinner...";
else cout<<name[1]<<" is the only one for you...";
return 0;
}
方法四:(Python)
#定义一个列表,存储姓名
name = []
#输入
while True:
n = input()
if n =='.': #输入.时结束输入
break
else:
name.append(n) #将键盘输入加到列表末尾
#输出
if len(name) < 2: #点赞人数不到2人的情况
print('Momo... No one is for you ...')
elif 2 <= len(name) < 14: #点赞人数超过2人不到14人的情况
print(name[1] + ' is the only one for you...')
else: #点赞人数超过14人的情况
print(name[1] + ' and ' + name[13] + ' are inviting you to dinner...')
诚挚希望有心者指正,渴望简单的方法。