题目描述
求两个多项式的乘积
pa=an*x^n + an-1*x^(n-1) + ... + a1*x + a0;
pb=bm*x^n + bm-1*x^(n-1) + ... + b1*x + b0;
其中,an,an-1...a1,a0,bm,bm-1...b1,b0都是整数,范围是[-1000,1000],0<=n,m<=1000。pa*pb的结果也是多项式,请你设计如何表示一个多项式,并写出两个多项式相乘的程序。
c++:
string multiplyPloynomial(const string&pA,const string&pB);
java:
String multiplyPloynomial(String pA,String pB);
其中pA和pB的格式都是“(-3,5),(87,4),(93,3),(3,0)”,表示一个多项式为-3*x^5 + 87*x^4 + 93*x^3 + 3。输入都是合法的,除了数字,左右括号和逗号没有别的任何字符,并且幂次都是从高到低排列的,输出也要求是这样的标准格式。
pa=an*x^n + an-1*x^(n-1) + ... + a1*x + a0;
pb=bm*x^n + bm-1*x^(n-1) + ... + b1*x + b0;
其中,an,an-1...a1,a0,bm,bm-1...b1,b0都是整数,范围是[-1000,1000],0<=n,m<=1000。pa*pb的结果也是多项式,请你设计如何表示一个多项式,并写出两个多项式相乘的程序。
c++:
string multiplyPloynomial(const string&pA,const string&pB);
java:
String multiplyPloynomial(String pA,String pB);
其中pA和pB的格式都是“(-3,5),(87,4),(93,3),(3,0)”,表示一个多项式为-3*x^5 + 87*x^4 + 93*x^3 + 3。输入都是合法的,除了数字,左右括号和逗号没有别的任何字符,并且幂次都是从高到低排列的,输出也要求是这样的标准格式。
解题思路
采用链表来表示多项式,因为如果用数组有可能遇到稀疏问题,同时链表可以动态添加节点。
相乘时,采用hashmap来保存两两相乘的结果,最后在扫描一遍hashmap即可构造出多项式
代码
/**
* 多项式相乘,返回字符串
*
* @param pA
* @param pB
* @return
*/
public static String multiplyPloynomial(String pA, String pB) {
PloynomialNode pAHead = constructPloynomial(pA);
PloynomialNode pBHead = constructPloynomial(pB);
PloynomialNode pAnode = pAHead;
PloynomialNode pBnode = pBHead;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int power, ratio;
while (pAnode != null) {
while (pBnode != null) {
power = pAnode.power + pBnode.power;
if (map.containsKey(power)) {
ratio = map.get(power);
ratio += pAnode.ratio * pBnode.ratio;
map.put(power, ratio);
} else {
ratio = pAnode.ratio * pBnode.ratio;
map.put(power, ratio);
}
pBnode = pBnode.next;
}
pBnode = pBHead;
pAnode = pAnode.next;
}
Iterator iter = (Iterator) map.entrySet().iterator();
PloynomialNode preNode = null, nextNode = null;
// entry.getKey()是按从小到大排序的
// 如果仅按题意,遍历hashmap即可构造出返回的string,这里先构造出了多项式
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
power = (Integer) entry.getKey();
ratio = (Integer) entry.getValue();
if (ratio != 0) {
PloynomialNode node = new PloynomialNode(ratio, power);
if (nextNode != null) {
node.next = nextNode;
}
nextNode = node;
preNode = node;
}
}
return constructString(preNode);
}
/**
* 多项式构造字符串
*
* @param pA
* @return
*/
public static String constructString(PloynomialNode head) {
StringBuilder builder = new StringBuilder();
if (head == null)
return null;
builder.append("(");
builder.append(head.ratio);
builder.append(",");
builder.append(head.power);
builder.append(")");
head = head.next;
while (head != null) {
builder.append(",(");
builder.append(head.ratio);
builder.append(",");
builder.append(head.power);
builder.append(")");
head = head.next;
}
return builder.toString();
}
/**
* 通过字符串构造多项式
*
* @param pA
* @return
*/
public static PloynomialNode constructPloynomial(String pA) {
if (pA == null) {
return null;
}
pA = pA.replace("(", "");
pA = pA.replace(")", "");
String[] s = pA.split(",");
PloynomialNode head = new PloynomialNode(Integer.valueOf(s[0]),
Integer.valueOf(s[1]));
PloynomialNode lastNode = head;
for (int i = 2; i < s.length; i += 2) {
PloynomialNode node = new PloynomialNode(Integer.valueOf(s[i]),
Integer.valueOf(s[i + 1]));
lastNode.next = node;
lastNode = node;
}
return head;
}
/**
* 多项式结点定义
*/
public static class PloynomialNode {
/** 系数 */
int ratio;
/** 幂次 */
int power;
PloynomialNode next;
public PloynomialNode() {
this(0, 0);
}
public PloynomialNode(int ratio, int power) {
this.ratio = ratio;
this.power = power;
this.next = null;
}
}