4.3 相同生日


题目

【问题描述】

在一个有n个人的大班级中,存在两个人生日相同的概率非常大,现给出每个学生的学号,出生月日,试找出所有生日相同的学生。

【输入形式】

第一行为整数n,表示有n个学生,n<=200。此后每行包含一个字符串和两个整数,分别表示学生的学号(字符串长度为11位)和出生月(1<=m<=12)日(1<=d<=31),学号、月、日之间用一个空格分隔。

【输出形式】

对每组生日相同的学生,输出一行,其中前两个数字表示月和日,后面跟着所有在当天出生的学生的学号,数字、学号之间都用一个空格分隔。对所有的输出,要求按日期从前到后的顺序输出。对生日相同的学号,按输入的顺序输出。

【样例输入】

6
07101020105 3 15
07101020115 4 5
07101020118 3 15
07101020108 4 5
07101020111 4 5
07101020121 8 10

【样例输出】

3 15 07101020105 07101020118
4 5 07101020115 07101020108 07101020111
8 10 07101020121

一、刚开始的困境

想到用生日做结构体,但是陷入学号不唯一如何存储加循环的问题

#include<bits/stdc++.h>
using namespace std;

struct birth{
	int month;
	int day;
};

int main()
{
	int n;
	cin>>n;
	map<string,bir>mp;
	for(int i=0;i<n;i++)
	{
		string x;
		birth bir;
		cin>>x>>bir.month>>bir.day;
		mp[x]=bir;
	}
	
	map<string,bir>::iterator it;
	
	for(it=mp.begin();it!=mp.end();it++)
	{
		
	}
	return 0;
} 

二、正确解法1(借鉴)

这个题目,一组月日可以对应多个学生,那么还是用map,只不过第一个模板参数是储存月日的pair(struct),第二个模板参数是储存所有对应月日生日学生的id(vector)

#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;

int main()
{           //<month,day>
    map<pair<int,int>,vector<string> > m;//第一个模板参数储存月日,第二个模板参数储存对应的所有学生
    map<pair<int,int>,vector<string> >::iterator it;
    vector<string>::iterator itv;
    int n;
    cin>>n;
    string id;
    int month,day;
    for(int i=0;i<n;i++)
    {
        cin>>id>>month>>day;
        //底下的{},是c++11的新做法,可以用{x,y}来赋值一个pair
        if(m.count({month,day})==0)//如果这个月日还没有学生生日
        {
            vector<string> v;
            v.push_back(id);
            m.insert({{month,day},v});//插入一个新的日期至map中
        }
        else m[{month,day}].push_back(id);//否则将该id插入到已存在的map中
    }
    for(it=m.begin();it!=m.end();it++)//遍历整个map,pair是按照first排序的,所以不用担心排序问题
    {
        cout<<it->first.first<<" "<<it->first.second<<" ";//输出月日
        for(itv=it->second.begin();itv!=it->second.end();itv++)//输出所有这一天生日的学生的学号
            cout<<*itv<<" ";
        cout<<endl;
    }
    system("pause");
    return 0;
}

正确解法2(借鉴)

#include<bits/stdc++.h>
using namespace std;

typedef vector<string> arrayid;

struct birthday{
	int mon=13;
	int day=32;    //此处开始设置时没有赋值,出现bug 为什么 
	arrayid ids;
};
bool cmp(birthday l, birthday r)
{
	if (l.mon != r.mon) return l.mon < r.mon;
	return l.day < r.day;
}
int have[12][31];

int main()
{
	memset(have,-1,sizeof have);
	int n;cin>>n;
	birthday info[n];  //每一行输入学号加生日当成数组的一个元素,然后用cmp比较info数组大小 
	int mon,day; string id;
	int p=0;
	for(int i=0;i<n;i++)
	{
	    birthday tem;
		cin>>id>>mon>>day;
		if(have[mon-1][day-1]==-1)
		{
			have[mon-1][day-1]=p;//exist 并且此处写成p是有重要意义的 
			tem.day=day;tem.mon=mon;
			tem.ids.push_back(id);
			info[p]=tem;
			p++;
		}
		else
		{
			birthday tem=info[have[mon-1][day-1]];  //此处若写为p-1 反例是15 2 3;16 2 3;17 8 9;18 2 3;18 2 3 
			tem.ids.push_back(id);
		}
	} 
	sort(info,info+n,cmp);
	for(int i=0;i<p;i++)
	{
		cout<<info[i].mon << ' ' << info[i].day << ' ';
		for(arrayid::iterator it=info[i].ids.begin();it!=info[i].ids.end();it++)
		{
			cout<<*it<<" "; 
		}
		cout<<endl;	
	}
	return 0;
} 

总结

1.map的键值对象还可以是vector–这里的学号很多,可以做成一个vector string

2.在map中如何插入东西的方法

3.在map寻找有无该键的方法

4.在dev-c++修改编译器为c++11的标准

5.方法二的整体思路还是(也就是info[n])
vector<birthday,arrayid> 用cmp排序,只是此处arrayid是数组,在写入和输出都有门道,并且由于相同生日,info的下标表示也比较特殊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值