-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path수식 최대화.cpp
90 lines (86 loc) · 2.39 KB
/
수식 최대화.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <string>
#include <vector>
#include <sstream>
#include <stdlib.h>
#include <iostream>
int visit[3];
using namespace std;
long long a,b;
char c;
long long Max;
void back(int x,string s){
cout<<s<<endl;
if(x==3){
// cout<<s<<endl;
long long t = stoll(s);
if(0>t){
t*=-1;
}
if(Max<t){
Max=t;
}
}else{
for(int i=0;i<3;i++){
if(visit[i]==0){
visit[i]=1;
string tmp = s;
stringstream str(tmp);
string n = "";
str>>b;
if(i==0){
a=b;
while(str>>c&&str>>b){
// cout<<c<<" "<<b<<endl;
if(c=='+'){
a+=b;
}else{
n+=to_string(a);
n+=c;
a=b;
}
}
n+=to_string(a);
// cout<<n<<endl;
back(x+1,n);
}else if(i==1){
a=b;
while(str>>c&&str>>b){
// cout<<c<<" "<<b<<endl;
if(c=='-'){
a-=b;
}else{
n+=to_string(a);
n+=c;
a=b;
}
}
n+=to_string(a);
// cout<<n<<endl;
back(x+1,n);
}else{
a=b;
while(str>>c&&str>>b){
// cout<<c<<" "<<b<<endl;
if(c=='*'){
a=a*b;
}else{
n+=to_string(a);
n+=c;
a=b;
}
}
n+=to_string(a);
// cout<<n<<endl;
back(x+1,n);
}
visit[i]=0;
// cout<<n<<endl;
}
}
}
}
long long solution(string expression) {
long long answer = 0;
back(0,expression);
return Max;
}