输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式: 输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
输入样例:
4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1 5 20 -4 4 -5 2 9 1 -2 0
其他:
- 时间限制:200ms
- 内存限制:64MB
- 代码长度限制:16kB
- 解题思路:
按照多项式的乘法和加法规则来编写代码即可,只是在选用数据结构的时候看您要怎么选。
可以用数组实现,因为输入里已经确定数组的大小了,但是因为是复习巩固链表知识,所以我采用了单链表来存储数据,单链表的工作方式和队列一样FIFO,所以代码中创建了一个Queue类。这个队列到底是什么样的,其实是仁者见仁,智者见智。我个人认为当你用到它们的时候可以方便您地这个数据结构就是好的,不一定要和教科书上的一模一样。
当队列为空的时候,是这样子的:
当插入元素的时候是这个样子的:
头节点(front节点)的数据域始终是空的;尾节点(rear节点)的next域始终是空的,当队列为空时,数据域也是空的,但插入数据时从尾节点开始插入,所以队列不空,尾节点的数据域也不空。
队列的操作可以按需来写。
Jvav代码如下:
package cn.eee.www;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
//读入多项式,以指数递降方式输入,非零项系数
Scanner sc=new Scanner(System.in);
Queue queueA=readNomal(sc);
Queue queueB=readNomal(sc);
Queue MulResult=mulQ(queueA,queueB);//多项式乘法计算
MulResult.printQ();//输出计算后的多项式
Queue AddResult=addQ(queueA,queueB);//多项式加法计算
AddResult.printQ();//输出计算后的多项式
}
//读入多项式的方法
public static Queue readNomal(Scanner sc){
int m=sc.nextInt();
Queue queue=new Queue();
for(int i=0;i<m;i++){
Node node=new Node(sc.nextInt(),sc.nextInt());
queue.enQueue(node);
}
return queue;
}
//多项式相乘的方法
public static Queue mulQ(Queue queueA,Queue queueB){
Queue queueMul=new Queue();
Node nodeACurrent=queueA.getFistNode();
Node nodeBCurrent=queueB.getFistNode();
while(nodeACurrent!=null){//第二个多项式的第一项和第一个多项式的所有项相乘,得到初始多项式
queueMul.enQueue(new Node(nodeACurrent.getCoe()*nodeBCurrent.getCoe(),nodeACurrent.getExp()+nodeBCurrent.getExp()));
nodeACurrent=nodeACurrent.next;
}
nodeBCurrent=nodeBCurrent.next;
while(nodeBCurrent!=null){//queueB中每一个元素和queueA相乘
nodeACurrent=queueA.getFistNode();
while(nodeACurrent!=null){//queueB中某一个元素和queueA中每一个元素相乘
Node nodeMul=new Node(nodeBCurrent.getCoe()*nodeACurrent.getCoe(),nodeBCurrent.getExp()+nodeACurrent.getExp());//待插入的节点
//在初始多项式中找插入的位置并且插入到多项式中
Node nodeMulCurrent=queueMul.getFistNode();
Node nodeMulPrev=queueMul.getFront();
while(nodeMulCurrent!=null){
if(nodeMul.getExp()<nodeMulCurrent.getExp()){
if(nodeMulCurrent==queueMul.getRear()){//指数比rear节点小,则插入到rear节点后面
nodeMulCurrent.next=nodeMul;
queueMul.setRear(nodeMul);
break;
}else{
nodeMulCurrent=nodeMulCurrent.next;
nodeMulPrev=nodeMulPrev.next;
}
}else if(nodeMul.getExp()==nodeMulCurrent.getExp()){//合并同类项,插入
if((nodeMul.getCoe()+nodeMulCurrent.getCoe())==0){//合并系数是零就要删除
if(nodeMulCurrent==queueMul.getRear()){//要删除的这个节点是rear节点
if(nodeMulPrev==queueMul.getFront()){//要删除的这个节点是多项式的rear节点,并且rear节点上唯一的数据节点,则把数据域赋值为零就行
nodeMulCurrent.setCoe(0);
nodeMulCurrent.setExp(0);
}else if(nodeMulPrev!=queueMul.getFront()){//要删除的这个rear节点前面还有数据节点,则rear节点指向前一个节点(直接删除该节点)
queueMul.setRear(nodeMulPrev);
nodeMulPrev.next=null;
}
}else{//要删除的这个节点不是rear节点,直接删除
nodeMulPrev.next=nodeMulCurrent.next;
}
}else{//插入
nodeMulCurrent.setCoe(nodeMul.getCoe()+nodeMulCurrent.getCoe());
}
break;
}else if(nodeMul.getExp()>nodeMulCurrent.getExp()){
//插入到该节点的前面
nodeMul.next=nodeMulCurrent;
nodeMulPrev.next=nodeMul;
break;
}
}
nodeACurrent=nodeACurrent.next;
}
nodeBCurrent=nodeBCurrent.next;
}
return queueMul;
}
//多项式相加的方法
public static Queue addQ(Queue queueA,Queue queueB){
Queue queueAdd=new Queue();
while((!queueA.isEmpty())&&(!queueB.isEmpty())){
Node currentA=queueA.getFistNode();
Node currentB=queueB.getFistNode();
if(currentA.getExp()>currentB.getExp()){
Node temp=new Node(currentA.getCoe(),currentA.getExp());
queueAdd.enQueue(temp);queueA.deQueue();
}else
if(currentA.getExp()<currentB.getExp()){
Node temp=new Node(currentB.getCoe(),currentB.getExp());
queueAdd.enQueue(temp);
queueB.deQueue();
}else
if(currentA.getExp()==currentB.getExp()){
Node temp=new Node(currentA.getCoe()+currentB.getCoe(),currentA.getExp());
if(temp.getCoe()!=0){
queueAdd.enQueue(temp);
}
queueA.deQueue();
queueB.deQueue();
}
}
while(!queueA.isEmpty()){
Node currentA=queueA.getFistNode();
Node temp=new Node(currentA.getCoe(),currentA.getExp());
queueAdd.enQueue(temp);
queueA.deQueue();
}
while(!queueB.isEmpty()){
Node currentB=queueB.getFistNode();
Node temp=new Node(currentB.getCoe(),currentB.getExp());
queueAdd.enQueue(temp);
queueB.deQueue();
}
return queueAdd;
}
}
class Node{//链表节点类
private int coe;//系数
private int exp;//指数
Node next;
public Node(int c,int e){
this.coe=c;
this.exp=e;
this.next=null;
}
public Node(){
this.coe=0;
this.exp=0;
this.next=null;
}
public int getCoe(){
return this.coe;
}
public int getExp(){
return this.exp;
}
public void setCoe(int c){
this.coe=c;
}
public void setExp(int e){
this.exp=e;
}
}
class Queue{//用单向链表实现的队列(头节点的数据域是空的)
private Node front;
private Node rear;
public Queue(){//创建一个头节点(元素为空)指向尾节点(元素为空)的空队列
this.front=new Node();
this.rear=new Node();
this.front.next=this.rear;
}
public boolean isEmpty(){
if(this.rear.getCoe()==0){
return true;
}else{
return false;
}
}
public void enQueue(Node node){//从队尾插入一个节点(入队)
if(this.isEmpty()){
this.front.next=node;
this.rear=node;
}else{
this.rear.next=node;
node.next=null;
this.rear=node;
}
}
public void deQueue(){//从队头删除节点(出队)
Node node=this.front;
if(this.isEmpty()){
System.out.println("空队列,不可删除节点");
}else if(this.getFistNode()==this.rear && this.rear.getCoe()!=0){
this.rear.setCoe(0);
this.rear.setExp(0);
}else{
node.next=this.getFistNode().next;
}
}
public Node getFistNode(){
return this.front.next;
}
public Node getFront(){
return this.front;
}
public Node getRear(){
return this.rear;
}
public void setFront(Node node){
this.front=node;
}
public void setRear(Node node){
this.rear=node;
}
public void printQ(){
Queue q=this;
if(q.isEmpty()){
System.out.print(0+" "+0);
}else{
Node node=q.getFistNode();
System.out.print(node.getCoe()+" "+node.getExp());
q.deQueue();
while(!q.isEmpty()){
node=q.getFistNode();
System.out.print(" "+node.getCoe()+" "+node.getExp());
q.deQueue();
}
}
System.out.println();
}
}
输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。
输出格式: 输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0
。
输入样例:
4 3 4 -5 2 6 1 -2 0 3 5 20 -7 4 3 1
输出样例:
15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1 5 20 -4 4 -5 2 9 1 -2 0
- 其他:
- 时间限制:200ms
- 内存限制:64MB
- 代码长度限制:16kB
- 解题思路:
按照多项式的乘法和加法规则来编写代码即可,只是在选用数据结构的时候看您要怎么选。
可以用数组实现,因为输入里已经确定数组的大小了,但是因为是复习巩固链表知识,所以我采用了单链表来存储数据,单链表的工作方式和队列一样FIFO,所以代码中创建了一个Queue类。这个队列到底是什么样的,其实是仁者见仁,智者见智。我个人认为当你用到它们的时候可以方便您地这个数据结构就是好的,不一定要和教科书上的一模一样。
当队列为空的时候,是这样子的:
当插入元素的时候是这个样子的:
头节点(front节点)的数据域始终是空的;尾节点(rear节点)的next域始终是空的,当队列为空时,数据域也是空的,但插入数据时从尾节点开始插入,所以队列不空,尾节点的数据域也不空。
队列的操作可以按需来写。 JAVA代码如下