-
Notifications
You must be signed in to change notification settings - Fork 0
/
3D.cpp
69 lines (64 loc) · 1.37 KB
/
3D.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
#include<iostream>
#include <string>
using namespace std;
string num;
int len, cnt = 2147483647;
bool pre = false;
void handle(char a, char b);
int main()
{
cin >> num;
len = num.length();
handle('0', '0');
handle('2', '5');
handle('7', '5');
handle('5', '0');
if(pre){
cout << cnt << endl;
}else{
cout << -1 << endl;
}
return 0;
}
void handle(char a, char b){
string tem = num;
int flag = 0, cnt1 = 0;
for(int i = len - 1; i >= 0; i--){
if(tem[i] == b){
flag++;
for(int j = i; j < len - 1; j++){
swap(tem[j], tem[j + 1]);
cnt1++;
}
break;
}
}
for(int i = len - 2; i >= 0; i--){
if(tem[i] == a){
flag++;
for(int j = i; j < len - 2; j++){
swap(tem[j], tem[j + 1]);
cnt1++;
}
break;
}
}
if(flag == 2){
if(tem[0] == '0'){
for(int i = 1; i< len - 2; i++){
if(tem[i] != '0'){
cnt1 += i;
pre = true;
if(cnt > cnt1){
cnt = cnt1;
}
}
}
}else{
pre = true;
if(cnt > cnt1){
cnt = cnt1;
}
}
}
}