-
Notifications
You must be signed in to change notification settings - Fork 73
/
roman.cxx
60 lines (54 loc) · 1.44 KB
/
roman.cxx
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
#include <algorithm>
#include <utility>
#include <string>
#include <vector>
#include <cstdlib>
#include <cuda_runtime.h>
// Convert ints to roman numerals.
// http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B
inline std::string to_roman(int value) {
struct romandata_t { int value; char const* numeral; };
static romandata_t const romandata[] =
{ 1000, "M",
900, "CM",
500, "D",
400, "CD",
100, "C",
90, "XC",
50, "L",
40, "XL",
10, "X",
9, "IX",
5, "V",
4, "IV",
1, "I",
0, NULL }; // end marker
std::string result;
for (romandata_t const* current = romandata; current->value > 0; ++current)
{
while (value >= current->value)
{
result += current->numeral;
value -= current->value;
}
}
return result;
}
__global__ void test_vector(int count) {
// Generate count random integers.
std::vector<std::pair<std::string, int> > romans(count);
for(int i : count) {
// Don't know how to count higher than 4000.
int x = rand() % 4000;
romans[i] = std::make_pair(to_roman(x), x);
}
// Now sort them according to lexicographical order of their
// roman numerals.
std::sort(romans.begin(), romans.end());
// Print the sorted roman numerals.
printf("%4d - %s\n", romans[:].second, romans[:].first.c_str())...;
}
int main() {
test_vector<<<1, 1>>>(20);
cudaDeviceSynchronize();
}