PTA 团体程序设计天梯赛-练习集 L1-035 情人节(15 分) java python

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 ...

 用Java写

import java.util.ArrayList;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		ArrayList arr = new ArrayList();
		while (true) {
			String t = sc.nextLine();
			if (t.equals("."))
				break;
			arr.add(t);
		}
		if(arr.size()>=13)
		System.out.println(arr.get(1)+" and "+arr.get(13)+" are inviting you to dinner...");
		else if(arr.size()>=2&&arr.size()<13)
		System.out.println(arr.get(1)+" is the only one for you...");
		else 
		System.out.println("Momo... No one is for you ...");
	}
}
  • 用arr.size()方法计算集合的长度,就是点赞的人数
  • 用arr.get()方法来根据索引来取值

用Python写

ls = []
while 1:
    t = input()
    if t == ".":
        break
    ls.append(t)

if len(ls) >= 13:
    print(ls[1] + " and " + ls[13] + " are inviting you to dinner...")
elif len(ls) >= 2 and len(ls) < 13 :
    print(ls[1] + " is the only one for you...")
else :
    print("Momo... No one is for you ...")

 

你可能感兴趣的:(PTA程序设计)