题意:
一个
n
,
(
1
≤
n
≤
100
)
n,(1 \le n \le 100)
n,(1≤n≤100),然后
n
n
n行,每行都是 FirstName LastName Contact
,三个字符串,Contact
表示电话(由十个数字组成)或邮箱(必须包含一个@
和一个.
),
n
=
0
n=0
n=0 结束。
输出按照下面样例的格式,输出要排序,输出的联系人先按FirstName
再按LastName
升序排序,电话和邮箱升序排序。
思路:
定义一个结构体person
表示每一个人,
struct person
{
string FirstName, LastName;
vector<string> phone;
vector<string> mail;
void Sort();
};
再用一个map<string, int>
对每个名字FirstName + LastName
进行标号。
样例输入
6
John Doe 4071234567
Bill Smith bsmith@somewhere.com
Bill Smith 1231231234
John Doe John.Doe@my.house.net
John Doe John.Doe@work.com
Bill Smith 1234567890
2
Bone Head 1231231234
Air Head airhead@my.house.net
0
样例输出
Contact list #1:
John Doe
Phone:
(407)123-4567
E-Mail:
John.Doe@my.house.net
John.Doe@work.com
###
Bill Smith
Phone:
(123)123-1234
(123)456-7890
E-Mail:
bsmith@somewhere.com
###
Contact list #2:
Air Head
Phone:
E-Mail:
airhead@my.house.net
###
Bone Head
Phone:
(123)123-1234
E-Mail:
###
Code:
#include <bits/stdc++.h>
using namespace std;
struct person
{
string FirstName, LastName;
vector<string> phone;
vector<string> mail;
void Sort()
{
sort(phone.begin(), phone.end());
sort(mail.begin(), mail.end());
}
};
bool cmp(person A, person B)
{
if (A.LastName == B.LastName)
return A.FirstName < B.FirstName;
else
return A.LastName < B.LastName;
}
int main()
{
int n, Case = 0;
while (cin >> n, n)
{
person p[n + 1];
string firstname, lastname, contact;
map<string, int> m;
m.clear();
int cnt = 0;
for (int i = 0; i < n; i++)
{
cin >> firstname >> lastname >> contact;
string name = firstname + lastname;
if (m.find(name) == m.end())
{
cnt++;
m[name] = cnt;
p[cnt].FirstName = firstname;
p[cnt].LastName = lastname;
p[cnt].phone.clear();
p[cnt].mail.clear();
}
if (contact.find('@') != -1)
p[m[name]].mail.push_back(contact);
else
p[m[name]].phone.push_back(contact);
}
for (int i = 1; i <= cnt; i++)
p[i].Sort();
sort(p + 1, p + cnt + 1, cmp);
Case++;
cout << "Contact list #" << Case << ":\n";
for (int i = 1; i <= cnt; i++)
{
cout << p[i].FirstName << " " << p[i].LastName << "\n";
cout << "Phone:\n";
for (int j = 0; j < p[i].phone.size(); j++)
{
string temp = p[i].phone[j];
cout << "(" << temp.substr(0, 3) << ")" << temp.substr(3, 3) << "-" << temp.substr(6) << "\n";
}
cout << "E-Mail:\n";
for (int j = 0; j < p[i].mail.size(); j++)
{
cout << p[i].mail[j] << "\n";
}
cout << "###\n";
}
cout << endl;
}
return 0;
}