赞
踩
设计函数分别求两个一元多项式的乘积与和。
输入分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
- if(curPc.next==null && is) {
- curPc.next = new Node(xs, zs);
- }
- import java.util.Scanner;
-
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int lengthA = in.nextInt();
- int[][] numA = new int[lengthA][2];
- for (int i = 0; i < numA.length; i++) {
- for (int j = 0; j < numA[i].length; j++) {
- numA[i][j] = in.nextInt();
- }
- }
- LinkList linkListA = new LinkList(numA);
- int lengthB = in.nextInt();
- int[][] numB = new int[lengthB][2];
- for (int i = 0; i < numB.length; i++) {
- for (int j = 0; j < numB[i].length; j++) {
- numB[i][j] = in.nextInt();
- }
- }
- LinkList linkListB = new LinkList(numB);
- Node mutiply = linkListA.hbMutiplly(linkListA, linkListB);
- Node sum = linkListA.hbSum(linkListA, linkListB);
-
- mutiply = mutiply.next;
- boolean is = true;//是不是第一个元素
- boolean isZero = true;
- while(mutiply!=null) {
- if(is) {
- if(mutiply.xs!=0) {
- System.out.print(mutiply.xs+" "+mutiply.zs);
- is = false;
- isZero = false;
- }
- }else {
- if(mutiply.xs!=0) {
- System.out.print(" "+mutiply.xs+" "+mutiply.zs);
- isZero = false;
- }
- }
- mutiply = mutiply.next;
- }
- if(isZero) {
- System.out.print(0+" "+0);
- }
- System.out.println();
- is = true;
- isZero = true;
- sum = sum.next;
- while(sum!=null) {
- if(is) {
- if(sum.xs!=0) {
- System.out.print(sum.xs+" "+sum.zs);
- is = false;
- isZero = false;
- }
- }else {
- if(sum.xs!=0) {
- System.out.print(" "+sum.xs+" "+sum.zs);
- isZero = false;
- }
- }
- sum = sum.next;
- }
- if(isZero) {
- System.out.print("0 0");
- }
- System.out.println();
- }
- }
-
- class LinkList {
- Node head;// 头结点
- /**
- * 多项式乘积
- * @param linkListA
- * @param linkListB
- * @return
- */
- public Node hbMutiplly(LinkList linkListA,LinkList linkListB) {
- Node pa = linkListA.head.next;
- Node pb = linkListB.head.next;
- Node pc = new Node();
- Node curPc = pc;
- //初始化
- if(pa!=null) {
- while(pb!=null) {
- curPc.next = new Node(pa.xs*pb.xs, pa.zs+pb.zs);
- curPc = curPc.next;
- pb = pb.next;
- }
- pa = pa.next;
- }
-
- while(pa!=null) {
- pb = linkListB.head.next;
- curPc = pc;
- while(pb!=null) {
- int xs = pa.xs*pb.xs;
- int zs = pa.zs+pb.zs;
- boolean is = true;
- while(curPc.next!=null) {
- if(curPc.next.zs==zs) {
- if((curPc.next.xs+xs)!=0) {
- curPc.next.xs = curPc.next.xs+xs;
- }else {
- curPc.next = curPc.next.next;
- is = false;
- }
- break;
- }else if(curPc.next.zs<zs) {
- curPc.next = new Node(xs, zs, curPc.next);
- break;
- }
- curPc = curPc.next;
- }
- if(curPc.next==null && is) {
- curPc.next = new Node(xs, zs);
- }
-
- // Node c = pc.next;
- // while (c!= null) {
- // System.out.print(c.xs + " " + c.zs + " ");
- // c = c.next;
- // }
- // System.out.println();
-
- pb = pb.next;
- }
- pa = pa.next;
- }
- return pc;
- }
-
- /**
- * 多项式加法
- * @param linkListA
- * @param linkListB
- * @return
- */
- public Node hbSum(LinkList linkListA, LinkList linkListB) {
- Node pa = linkListA.head.next;
- Node pb = linkListB.head.next;
- Node NodeCHead = new Node();// 存放多项式的和
- Node pc = NodeCHead;
-
- while (pa != null && pb != null) {
- if (pa.zs == pb.zs) {
- if ((pa.xs + pb.xs) != 0) {
- pc.next = new Node(pa.xs + pb.xs, pa.zs);
- pc = pc.next;
- }
- pa = pa.next;
- pb = pb.next;
- } else if (pa.zs > pb.zs) {
- pc.next = pa;
- pc = pc.next;
- pa = pa.next;
- } else {
- pc.next = pb;
- pc = pc.next;
- pb = pb.next;
- }
- }
- pc.next = (pa != null) ? pa : pb;
- return NodeCHead;
- }
-
- public void display() {
- Node p = head.next;
- while (p != null) {
- System.out.print(p.xs + " " + p.zs + " ");
- p = p.next;
- }
- System.out.println();
- }
-
- public LinkList() {
- this.head = new Node();
- }
-
- public LinkList(int[][] num) {
- this();
- Node p = head;
- for (int i = 0; i < num.length; i++) {
- p.next = new Node(num[i][0],num[i][1], p.next);
- p = p.next;
- }
- }
-
- }
-
- class Node {
- int xs;
- int zs;
- Node next;
-
- /**
- * 初始化头结点
- */
- public Node() {
- this.next = null;
- }
-
- public Node(int xs, int zs, Node next) {
- this(xs, zs);
- this.next = next;
- }
-
- /**
- * 尾结点
- *
- * @param xs
- * @param zs
- */
- public Node(int xs, int zs) {
- this();
- this.xs = xs;
- this.zs = zs;
- }
- }
序号 | 输入 | 输出 |
---|---|---|
1 | 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 |
2 | 2 1 2 1 0 2 1 2 -1 0 | 1 4 -1 0 2 2 |
3 | 2 -1000 1000 1000 0 2 1000 1000 -1000 0 | -1000000 2000 2000000 1000 -1000000 0 0 0 |
4 | 0 1 999 1000 | 0 0 999 1000 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。