1 条题解

  • 0
    @ 2025-8-24 22:35:21

    自动搬运

    查看原文

    来自洛谷,原作者为

    avatar Lyrella
    倒闭了,自闭了|羊角包 & 锦依卫|我也想要金钩惹~

    搬运于2025-08-24 22:35:21,当前版本为作者最后更新于2022-10-16 17:20:12,作者可能在搬运后再次修改,您可在原文处查看最新版

    自动搬运只会搬运当前题目点赞数最高的题解,您可前往洛谷题解查看更多

    以下是正文


    题意

    给你一串狮子,让你先带值,再求值。(类似于表达式求值)

    思路

    与表达式求值一样,直接用一个 stack\text{stack} 来处理运算。具体讲就是把运算符和数字存在 stack\text{stack} 里,如果遇到 ")"\text{")"} 就把它和与它匹配的前括号之间的狮子暴力算出来。如果是 "-"\text{"-"} 就把数字累加,如果是 "|"\text{"|"} 就把数字的倒数累加再用 1\text{1} 来除。

    注意

    • double\text{double}
    • 精度

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int N = 1e5 + 5;
    int n;
    stack < double > st;
    double R[15];
    string c;
    double sum()
    {
    	double h = st.top(); st.pop();
    	if(st.top() == - 4) while(st.top() != - 1){st.pop(); h += st.top(); st.pop();}
    	else
    	{
    		h = 1.0 / h;
    		while(st.top() != -1){st.pop(); h += 1.0 / st.top(); st.pop();}
    		h = 1.0 / h;
    	}
    	st.pop();
    	return h;
    }
    signed main()
    {
    	scanf("%d", &n);
    	for(int i = 1; i <= n; i++)scanf("%lf", &R[i]);
    	cin >> c;
    	for(int i = 0; i < c.size(); i++)
    	{
    		if(c[i] == '(')st.push(- 1);
    		if(c[i] == ')')st.push(sum());
    		if(c[i] == '|')st.push(- 3);
    		if(c[i] == '-')st.push(- 4);
    		if(c[i] >= '0' and c[i] <= '9')st.push(R[c[i] - '0']);
    	}
    	printf("%.8lf", st.top());
    	return 0;
    }
    
    • 1

    信息

    ID
    7388
    时间
    1000ms
    内存
    64MiB
    难度
    3
    标签
    递交数
    0
    已通过
    0
    上传者