整道题理解起来并不难,先是通过遍历找到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;
}
}