-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.cpp
More file actions
executable file
·50 lines (43 loc) · 1.09 KB
/
test2.cpp
File metadata and controls
executable file
·50 lines (43 loc) · 1.09 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
#include <cstdio>
#include <cstdlib>
#include <string>
#include <map>
using std::map;
using std::string;
using std::make_pair;
class Solution {
public:
string largestNumber(vector<int> &num) {
map<string, int> smap;
for (vector<int>::iterator it = num.begin();
it != num.end(); it++) {
char s[64] = {0};
snprintf(s, sizeof(s), "%u", *it);
smap.insert(make_pair(s, *it));
}
string result;
for (map<string, int>::reverse_iterator it = smap.rbegin();
it != smap.end(); it++) {
result += it->first;
}
return result;
}
};
int main() {
const int l[] = {3, 30, 34, 5, 9};
const int len = sizeof(l) / sizeof(int);
map<string, int> smap;
for (int i = 0; i < len; i++) {
char s[32] = {0};
snprintf(s, sizeof(s), "%u", l[i]);
smap.insert(make_pair(s, l[i]));
}
string result;
for (map<string, int>::reverse_iterator it = smap.rbegin();
it != smap.rend(); it++) {
result += it->first;
printf("key->value: %s -> %d\n", it->first.c_str(), it->second);
}
fprintf(stdout, "%s\n", result.c_str());
return 0;
}