本文介绍了如何从多项式表达式中得出系数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在输入处,我得到一个多项式作为字符串,我想获取变量中的系数,但我不知道该怎么做.例如:7x^4+3x^3-6x^2+x-8
.最大度未知,系数是整数.我将非常感谢您的帮助.
At the input I get a polynomial as a string,I want to get its coefficients in variables, but i have no idea gow do this.example:7x^4+3x^3-6x^2+x-8
.Maximum degree is not known, coefficients are integers.I will be very grateful for any help.
推荐答案
用加号和减号(例如,用 re.split ),将结果中的符号保留下来.然后,对于每个子字符串,用"x"除以得到前导系数(+1和-1是特殊情况),并记下x的次幂(即系数0).
Split by plus and minus (e.g. with re.split), preserving the signs in the results. Then for each substring, split by "x" to get the leading coefficient (+1 and -1 are special cases), and take note of missing powers of x (i.e. coefficient 0).
这篇关于如何从多项式表达式中得出系数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!