Alnwicker 2023-04-03 22:52 采纳率: 100%
浏览 23
已结题

关于#结构体#的问题,如何解决?(语言-c++)

您好,这是我在编程过程中遇到的问题,我的代码有错误,希望您能够帮忙指出,并附上正确的,能通过的代码,谢谢。
【题目描述】一个有180人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的名字,出生月日。试找出所有生日相同的学生。
【Input】第一行为整数n,表示有n个学生,n ≤ 180。此后每行包含一个字符串和两个整数,分别表示学生的名字(名字第一个字母大写,其余小写,不含空格,且长度小于20)和出生月(1 ≤ m ≤ 12)日(1 ≤ d ≤ 31)。名字、月、日之间用一个空格分隔。
【Output】每组生日相同的学生,输出一行,其中前两个数字表示月和日,后面跟着所有在当天出生的学生的名字,数字、名字之间都用一个空格分隔。对所有的输出,要求按日期从前到后的顺序输出。 对生日相同的名字,按名字从短到长按序输出,长度相同的按字典序输出。如没有生日相同的学生,输出”None”。


#include <bits/stdc++.h>
using namespace std;
int num=0;
struct samebirth{
    string name;
    float month;
    float day;
};    
struct samebirth student[200];
int main(){
    int n;//学生的数量
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>student[n];
    sort(student[1].month.day,student[1+n].month.day);
    if(student[n].month.day==student[n+1].month.day) num++;
    }
    if(num>=2){
        for(int i=0;i<n;i++){
            cout<<student[n];
    }
    else cout<<"None"<<endl;
    return 0;
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2023-04-04 00:23
    关注
    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        int n;//学生的数量
        cin>>n;
    
        struct samebirth{
            string name;
            float month;
            float day;
        };    
        struct samebirth student[200];
    
        for(int i=0;i<n;i++){
            cin>>student[i].name>>student[i].month>>student[i].day;
        }
        sort(student, student + n, [](samebirth a, samebirth b){
            if (a.month == b.month) return a.day < b.day;
            else return a.month < b.month;
        });
    
        int num = 0;
        for(int i=0;i<n-1;i++){
            if(student[i].month==student[i+1].month&&student[i].day==student[i+1].day) {
                num++;
            }
        }
        if(num>=1){
            for(int i=0;i<n;i++){
                if(i<n-1&&student[i].month==student[i+1].month&&student[i].day==student[i+1].day){
                    cout<<student[i].name<<" "<<student[i].month<<" "<<student[i].day<<endl;
                }
                else if(i>0&&student[i].month==student[i-1].month&&student[i].day==student[i-1].day){
                    cout<<student[i].name<<" "<<student[i].month<<" "<<student[i].day<<endl;
                }
            }
        }
        else cout<<"None"<<endl;
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月12日
  • 已采纳回答 4月4日
  • 创建了问题 4月3日

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘