2016/9/8 1003. MJ, Nowhere to Hide

整道题理解起来并不难,先是通过遍历找到ip地址相同的,然后记录对应的两个id,进行排序后输出。根据排序方法的不同,用了两种记录。一种是用一个二维数组记录,然后直接冒泡排序;另外一种是用结构体记录,再自己写cmp用sort排序,都是很基本的东西,慢慢捡起来了。

// Problem#: 19205
// Submission#: 4805168
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
struct Match
{
	string id1;
	string id2;
};
bool cmp(Match a, Match b)
{
	return a.id2 < b.id2;
}
int main()
{
	string name[20];
	string id[20];
	Match find[20];
	for (;;)
	{
		int n;
		int count = 0;
		cin >> n;
		if (n == 0)break;
		for (int i = 0; i <= n - 1; i++)
		{
			cin >> name[i] >> id[i];
		}
		for (int i = 0; i <= n - 1; i++)
		{
			for (int j = i + 1; j <= n - 1; j++)
			{
				if (id[i] == id[j])
				{
					find[count].id1 = name[j];
					find[count].id2 = name[i];
					count++;
				}
			}
		}
		//for (int i = 0; i <= n - 1; i++) //对find的冒泡排序
		//{
		//	for (int j = 0; j <= n - 1; j++)
		//	{
		//		if (find[i][1]>find[j][1])
		//		{
		//			string temp1 = find[i][0];
		//			string temp2 = find[i][1];
		//			string temp3 = find[j][0];
		//			string temp4 = find[j][1];
		//			find[j][0] = find[i][0];
		//			find[j][1] = find[i][1];
		//			find[i][0] = temp3;
		//			find[i][1] = temp4;
		//		}
		//	}
		//}
		sort(find, find + count, cmp);
		for (int i = 0; i<= count-1; i++)
		{
			cout << find[i].id1 << " is the MaJia of " << find[i].id2 << endl;
		}
		cout << endl;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值