void go(int here, int sum) {
if(here == 마지막) {
ret = max(ret, sum);
return;
}
go(here + 1, 계산결과);
if(다음숫자가_2개이상_남았으면) {
go(here + 2, 계산결과);
}
}
#include <bits/stdc++.h>
using namespace std;
vector<int> num;
vector<char> oper_str;
int n;
int ret = INT_MIN;
string s;
int oper(char a, int b, int c) {
cout << "연산 수행: " << b << a << c << "=";
int result;
if(a == '+') result = b + c;
if(a == '-') result = b - c;
if(a == '*') result = b * c;
cout << result << '\n';
return result;
}
void go(int here, int _num) {
cout << "\n현재 위치(here): " << here << ", 현재까지의 계산값(_num): " << _num << '\n';
if(here == num.size() - 1) {
cout << "끝에 도달! 현재 ret: " << ret << ", 새로운 값: " << _num << "\n";
ret = max(ret, _num);
return;
}
cout << "괄호 없이 계산하는 경우, go(" << here + 1 << ", oper(" << oper_str[here] << ", " << _num << ", " << num[here + 1] << ")) 호출\n";
go(here + 1, oper(oper_str[here], _num, num[here + 1]));
if(here + 2 <= num.size() - 1) {
cout << "괄호 사용하는 경우\n";
cout << "괄호 안을 먼저 계산: " << num[here + 1] << oper_str[here + 1] << num[here + 2] << '\n';
int temp = oper(oper_str[here + 1], num[here + 1], num[here + 2]);
go(here + 2, oper(oper_str[here], _num, temp));
}
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> s;
for(int i = 0; i < n; i++) {
if(i % 2 == 0) {
num.push_back(s[i] - '0');
} else {
oper_str.push_back(s[i]);
}
}
cout << "\n 계산 시작: \n";
go(0, num[0]);
cout << "\n최종 결과: " << ret << '\n';
return 0;
}