我正在处理Kattis 问题,在这里我应该以前缀表示法接受输入,将其简化并以前缀表示法返回它。这些是输入和输出的示例:
Sample Input 1 Sample Output 1
+ 3 4 Case 1: 7
- x x Case 2: - x x
* - 6 + x -6 - - 9 6 * 0 c Case 3: * - 6 + x -6 - 3 * 0 c
Run Code Online (Sandbox Code Playgroud)
我已经编写了这段代码,并且如果使用这种输入数据运行它,我将获得与上述完全相同的输出。但是,我从Kattis那里得到了错误的答案。
我在这里做错了什么?由于没有任何调试提示,这令人沮丧。
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const operators = ['+', '-', '*', '/'];
const operatorsFunctions = {
'+': (a, b) => a …Run Code Online (Sandbox Code Playgroud) 我正在编写一个小型计算器(带有前缀表示法),我很好奇如何将前缀表示法转换为中缀表示法。我目前有一个功能,但它很奇怪,我不知道如何修复它。我所说的奇怪是指如果给予['+', x, y]它就会返回,(() + x + () + y)这让我感到困惑。这是代码。
def pre_in(read):
#print read
tempOp = read[0]
body = read[1:]
expr = []
for i in range(len(body)-1):
if not isinstance(body[i], list) and body[i] != " ":
expr.append(str(body[i]))
expr.append(tempOp)
else:
expr.append(str(pre_in(body[i])))
expr.append(tempOp)
try:
if not isinstance(body[-1], list):
expr.append(str(body[-1]))
else:
expr.append(str(pre_in(body[-1])))
except:
pass
if expr != None: return "("+' '.join(expr)+")"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
为什么该代码由于错误而无法编译:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
cout << ++(i++) << " " << i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然该代码确实编译:
#include <iostream>
using namespace std;
int main()
{
int i = 0;
cout << (++i)++ << " " << i << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不明白这个.从我的观点来看,编译第一个块是非常合理的.表达式++(i ++)只是意味着取i,递增它并输出,然后再递增它.
我不是在询问int溢出中的未定义行为.在写这个问题时我根本不知道r和l值,我不在乎为什么++我被认为是l值,但i ++不是.