C++每日刷题day2025.7.10

思路就是遇到数字就push进栈,遇到符号就取栈顶俩元素进行对应符号运算再push进栈中,直到栈中只剩一个数字。

cpp 复制代码
class Solution {
public:
    bool isNumber(string& x)
    {
        if (x == "+" || x == "-" || x == "*" || x == "/")
            return false;
        else return true;
    }
    int evalRPN(vector<string>& tokens) {
        stack<int> st;
        for (int i = 0; i < tokens.size(); ++i)
        {
            string& x = tokens[i];
            if (isNumber(x)) st.push(atoi(x.c_str()));
            else{
                int num1 = st.top();
                st.pop();
                int num2 = st.top();
                st.pop();
                switch (x[0])
                {
                    case '+' :{
                        st.push(num1 + num2);
                        break;
                    }
                    case '-' :{
                        st.push(num2 - num1);
                        break;
                    }
                    case '*' :{
                        st.push(num1 * num2);
                        break;
                    }
                    case '/' :{
                        st.push(num2 / num1);
                        break;
                    }
                    default:
                        break;
                }
            }
        }
        return st.top();
    }
};

来个辅助栈,一个正常的栈,另一个是只存放最小值的栈。

cpp 复制代码
class MinStack {
public:
    stack<int> st;
    stack<int> min_st;
    MinStack() {
        min_st.push(INT_MAX);
    }
    
    void push(int val) {
        st.push(val);
        min_st.push(min(val, min_st.top()));
    }
    
    void pop() {
        st.pop();
        min_st.pop();
    }
    
    int top() {
        return st.top();
    }
    
    int getMin() {
        return min_st.top();
    }
};

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack* obj = new MinStack();
 * obj->push(val);
 * obj->pop();
 * int param_3 = obj->top();
 * int param_4 = obj->getMin();
 */

我是用了一个堆一个优先级队列,优先级队列用来存放偶数的,栈用来存放奇数的,每次只需要取优先级队列/2再判断是不是偶数即可。

cpp 复制代码
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
using namespace std;

int main() {
    long long n, k;
    cin >> n >> k;
    
    // 使用 vector 动态分配内存
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    priority_queue<long long> st;
    stack<long long> other;
    for (long long i = 0; i < n; i++) {
        if (a[i] % 2 == 0) {
            st.push(a[i]);
        } else {
            other.push(a[i]);
        }
    }

    while (st.size() && k--) {
        long long top = st.top();
        st.pop();
        top /= 2;
        if (top % 2 == 0) st.push(top);
        else other.push(top);
    }

    long long sum = 0;
    while (!st.empty()) {
        sum += st.top();
        st.pop();
    }
    while (!other.empty()) {
        sum += other.top();
        other.pop();
    }
    cout << sum;

    return 0;
}
相关推荐
我材不敲代码4 小时前
Python实现打包贪吃蛇游戏
开发语言·python·游戏
身如柳絮随风扬4 小时前
Java中的CAS机制详解
java·开发语言
-dzk-6 小时前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
韩立学长6 小时前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
froginwe116 小时前
Scala 循环
开发语言
m0_706653236 小时前
C++编译期数组操作
开发语言·c++·算法
故事和你917 小时前
sdut-Java面向对象-06 继承和多态、抽象类和接口(函数题:10-18题)
java·开发语言·算法·面向对象·基础语法·继承和多态·抽象类和接口
Bruk.Liu7 小时前
(LangChain实战2):LangChain消息(message)的使用
开发语言·langchain
qq_423233907 小时前
C++与Python混合编程实战
开发语言·c++·算法
m0_715575347 小时前
分布式任务调度系统
开发语言·c++·算法