hyelie
hyelie
Hyeil Jeong
       
글쓰기    관리    수식입력
  • 전체보기 (495)
    • PS (283)
      • Algorithm (28)
      • PS Log (244)
      • Contest (6)
      • Tips (5)
    • Development (52)
      • Java (14)
      • Spring (23)
      • SQL (2)
      • Node.js (2)
      • Socket.io (3)
      • Study (4)
      • Utils (4)
    • DevOps (36)
      • Git (5)
      • Docker (4)
      • Kubernetes (2)
      • GCP (3)
      • Environment Set Up (8)
      • Tutorial (12)
      • Figma (2)
    • CS (74)
      • OOP (7)
      • OS (24)
      • DB (2)
      • Network (24)
      • Architecture (0)
      • Security (2)
      • Software Design (0)
      • Parallel Computing (15)
    • Project (15)
      • Project N2T (5)
      • Project ASG (0)
      • Project Meerkat (1)
      • Model Checking (7)
      • Ideas (2)
    • 내가 하고싶은 것! (34)
      • Plan (16)
      • Software Maestro (10)
      • 취준 (8)
hELLO · Designed By 정상우.
hyelie

hyelie

PS/PS Log

22.04.20. 풀었던 문제들

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
    hyelie
    hyelie

    티스토리툴바