-
Notifications
You must be signed in to change notification settings - Fork 0
/
LargestEvenNumber.cpp
56 lines (43 loc) · 896 Bytes
/
LargestEvenNumber.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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void swap(int& a, int& b){
int temp = a;
a = b;
b = temp;
}
int main() {
int test;
cin >> test;
while(test){
string s;
cin >> s;
int n = s.size(), mini = 10, ind = -1;
vector<int> temp(n);
for(int i = 0; i < n; i++){
int num = s[i] - '0';
temp[i] = num;
if(num%2 == 0){
if(num < mini){
mini = num;
ind = i;
}
}
}
if(ind == -1){
sort(temp.begin(), temp.end());
}
else{
swap(temp[0], temp[ind]);
sort(temp.begin()+1, temp.end());
}
for(int i = n-1; i >= 0; i--){
cout << temp[i];
}
cout << endl;
test--;
}
return 0;
}