1. 수식 최대화
값이 long long이니 string to number로 변환할 때 stol이 아닌, stoll로 변환해야 한다.
// 수식 최대화 #include <string> #include <vector> #include <iostream> #include <list> #include <iterator> using namespace std; // c가 operatior인지 판별 bool isOP(char c){ if(c == '+' || c == '-' || c == '*') return true; else return false; } // c는 operator, a와 b는 숫자. a와 b를 c연산 한 후 string으로 리턴 string cal_operand(string c, string a, string b){ long long result = 0, all = stoll(a), bll = stoll(b); if(c == "-"){ result = all - bll; }else if(c == "*"){ result = all * bll; }else if(c == "+"){ result = all + bll; } return to_string(result); } // list와 operator의 우선순위 ops가 주어졌을 때 결과 계산 후 리턴 long long calculate(list<string> l, string ops){ long long max_value = 0; list<string>::iterator iter, prv, nxt; for(int i = 0; i<ops.length(); i++){ string temp = ""; temp.push_back(ops[i]); for(iter = l.begin(); iter != l.end(); iter++){ if(*iter == temp){ prv = prev(iter, 1); nxt = next(iter, 1); *iter = cal_operand(*iter, *prv, *nxt); l.erase(prv); l.erase(nxt); } } } return abs(stoll(*l.begin())); } long long solution(string expression) { list<string> l; int length = expression.length(); for(int i = 0, prev = 0; i<length; i++){ prev = i; while(i < length && !isOP(expression[i])) i++; l.push_back(expression.substr(prev, i-prev)); if(i < length){ string temp = ""; temp.push_back(expression[i]); l.push_back(temp); } } vector<string> cases = {"*+-", "*-+", "-+*", "-*+", "+-*", "+*-"}; long long r = 0; for(int i = 0; i<cases.size(); i++){ r = max(r, calculate(l, cases[i])); } return r; }
2. 피로도
예전에는 '이걸 어떻게 풀지' 싶어서 넘긴 거였는데, 문제 조건을 다시 보니까 던전의 개수가 8이다. 즉 8! 안에서 해결할 수 있다는 말.... 이걸 못봤다.
next_permutation으로 순열을 돌면서 조건에 맞는지만 검사하면 되었다.
'PS > PS Log' 카테고리의 다른 글
22.05.01. 풀었던 문제들 (0) | 2022.06.23 |
---|---|
22.04.30. 풀었던 문제들 (0) | 2022.06.23 |
22.04.19. 풀었던 문제들 (0) | 2022.06.23 |
22.04.17. 풀었던 문제들 (0) | 2022.06.23 |
22.04.16. 풀었던 문제들 *** KMP 알고리즘 (0) | 2022.06.23 |