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

题目思路不难,下面分别给出我两次写题的代码 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i=0;
    string s[999];
   for(;;i++)
   {
    cin>>s[i];
    if(s[i]==".")
    {
        i--;//去掉读"."加的次数
        break;
    }
   }
   if(i<1)
   {
    cout<<"Momo... No one is for you ...";
   }
   else if(i<13)
   {
    cout<<s[1]<<" is the only one for you...";
   }
   else 
   {
    cout<<s[1]<<" and "<<s[13]<<" are inviting you to dinner...";
   }
   return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int cnt=0;
    string a,b;
    while(1)
    {
        string s;
        cin>>s;
        if(s==".") break;
        cnt++;
        if(cnt==2)
        {
            a=s;
        }
        if(cnt==14)
        {
            b=s;
        }
    }
    if(cnt<2) cout<<"Momo... No one is for you ...";
    else if(cnt<14) cout<<a<<" is the only one for you...";
    else cout<<a<<" and "<<b<<" are inviting you to dinner...";
    return 0;
}

 

### PTA 7-37 情人节 编程题解 情人节是一个特殊的日子,在信息时代被赋予了新的表达方式[^1]。对于PTA平台上的题目7-37,其核心在于处理一系列的情人节礼物分配问题。 #### 题目描述 给定N个人以及他们之间互赠的礼物数目,计算每个人收到的最大礼物数,并找出谁收到了最多的礼物。如果有多个人拥有相同的最大值,则按编号从小到大输出这些人。 #### 解决方案概述 为了高效解决这个问题,可以采用哈希表来记录每个人的礼物接收情况。遍历输入数据的同时更新这个表格,最后再遍历一次找到具有最高礼物数量的人选并按照要求排序输出结果。 #### Python 实现代码 下面提供了一个Python版本的具体实现方法: ```python def find_max_gift_receivers(n, gifts): from collections import defaultdict gift_counts = defaultdict(int) # 记录每人的礼物数量 for giver, receiver in gifts: gift_counts[receiver] += 1 max_gifts = max(gift_counts.values(), default=0) receivers_with_max_gifts = sorted( person for person, count in gift_counts.items() if count == max_gifts ) return receivers_with_max_gifts if __name__ == "__main__": n = int(input().strip()) gifts = [] for _ in range(n): line = list(map(int, input().split())) sender_id = line.pop(0) recipients_ids = set(line) - {0} # 去除可能存在的自送给自己或其他非法ID的情况 for recipient_id in recipients_ids: gifts.append((sender_id, recipient_id)) result = find_max_gift_receivers(n, gifts) print(' '.join(str(x) for x in result or ['None'])) ``` 此程序首先读取整型数值`n`表示参与者的总数,之后逐行解析参与者之间的关系形成一对多的形式存储于列表中;接着调用函数`find_max_gift_receivers()`完成主要逻辑运算得到最终的结果集;最后将符合条件的人物ID打印出来作为答案返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值