-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoman Sorting.cpp
More file actions
56 lines (46 loc) · 1.42 KB
/
Roman Sorting.cpp
File metadata and controls
56 lines (46 loc) · 1.42 KB
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> roman(n);
vector<int> nums(n);
for (int i = 0; i < n; i++) {
cin >> nums[i];
int x = nums[i];
string s = "";
while (x >= 1000) { s += "M"; x -= 1000; }
if (x >= 900) { s += "CM"; x -= 900; }
if (x >= 500) { s += "D"; x -= 500; }
if (x >= 400) { s += "CD"; x -= 400; }
while (x >= 100) { s += "C"; x -= 100; }
if (x >= 90) { s += "XC"; x -= 90; }
if (x >= 50) { s += "L"; x -= 50; }
if (x >= 40) { s += "XL"; x -= 40; }
while (x >= 10) { s += "X"; x -= 10; }
if (x == 9) { s += "IX"; x -= 9; }
if (x >= 5) { s += "V"; x -= 5; }
if (x == 4) { s += "IV"; x -= 4; }
while (x >= 1) { s += "I"; x -= 1; }
roman[i] = s;
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (roman[i] > roman[j]) {
string tempS = roman[i];
roman[i] = roman[j];
roman[j] = tempS;
int tempN = nums[i];
nums[i] = nums[j];
nums[j] = tempN;
}
}
}
for (int i = 0; i < n; i++) {
if (i) cout << " ";
cout << nums[i];
}
cout << endl;
}