题意与原解析均从本地 OpenJudge 缓存迁移。
OJ: noi_openjudge
题目 ID: ch0303-1696
难度:未知
标签:
日期: 2026-07-30 23:01
题意
完整题面见同目录的 problem.md。
思路
解析
非递归的写法,需要使用栈来实现,
如果栈最上面的三个元素,分别是
- 操作符
- 数字a
- 数字b
就可以直接运算得到结果c,然后加入到栈内,但是不好写代码,因为栈内的元素的类型不一样.
如果使用递归来写,代码就会变得很简单,递归本质就是栈.
代码
cpp
#include <iostream>
#include <cmath>
using namespace std;
char str[1000][100];
int cnt;
const int maxn = 1e5+5;
template<typename T = int,int siz = maxn>
struct mystack{
T sta[maxn];
int head = 0;
void clear() { head = 0;}
void push(T a) { sta[head++] = a;}
void pop(){head--;}
T top() { return sta[head-1];}
bool empty() { return head == 0;}
int size() { return head; }
};
mystack sta;
int main() {
while( cin >> str[cnt]) {
cnt++;
}
int new_idx = cnt;
for(int i = 0;i < cnt ;i++) {
if(str[i][0] >= '0' && str[i][0] <='9') {
}
}
// cout << num_sta.top();
printf("%f\n",num_sta.top());
return 0;
}