-
Notifications
You must be signed in to change notification settings - Fork 8
/
#1566 皇室成员姓名.cpp
87 lines (73 loc) · 1.13 KB
/
#1566 皇室成员姓名.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include <string>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 100005;
typedef struct Name {
string name;
string agestr;
int age;
}Name;
Name name[N];
int weight[300];
void initWeight()
{
//I V X L C D M
//1 5 10 50 100 500 1,000
weight['I'] = 1;
weight['V'] = 5;
weight['X'] = 10;
weight['L'] = 50;
weight['C'] = 100;
weight['D'] = 500;
weight['M'] = 1000;
}
/*
I V X L C D M
Value 1 5 10 50 100 500 1,000
*/
int getNum(string roma)
{
int l = roma.length();
int sum = 0;
for (int i = l - 1; i >= 0; --i)
{
if (i>0 && weight[roma[i - 1]]<weight[roma[i]])
{
sum += weight[roma[i]] - weight[roma[i - 1]];
i--;
}
else
{
sum += weight[roma[i]];
}
}
return sum;
}
bool cmp(Name a, Name b)
{
if (a.name != b.name)
return a.name < b.name;
return a.age < b.age;
}
int main()
{
initWeight();
int n;
cin >> n;
for (int i = 0; i<n; ++i)
{
string s;
cin >> s;
name[i].name = s;
cin >> s;
name[i].agestr = s;
name[i].age = getNum(s);
}
sort(name, name + n,cmp);
for (int i = 0; i<n; ++i)
{
cout << name[i].name << " " << name[i].agestr << endl;
}
}